2011-08-18 2 views
1

개체 목록의 ItemsSource가있는 comboBox가 있습니다. 따라서 DisplayMemberPath는 객체의 특정 속성으로 설정됩니다. 물론 이것은 올바른 값이 ComboBoxItem에 표시됨을 의미합니다.액세스 ComboBoxItem DisplayValue

제 문제는 XAML의 DisplayMemberPath에서 반환 된 "Value"를 가져 와서 다른 것으로 바인딩 할 수 있기를 바랍니다. 즉 ComboBoxItem에 "DisplayText"속성을 갖고 싶습니다.

물론 나는 이것을 가지고 있지 않으므로 ContentHost를 찾는 ComboBoxItem의 템플릿으로 이동하지 않고이 값을 얻을 수있는 방법을 아는 사람이 있습니까?

이 내 특정 사용에 관심이 있다면

, 나는 콤보 상자의 스타일에이 작업을 수행하려고 해요 : 당신은 그냥 바인딩하는 경우 잘 작동 Path=Content 물론

.... 
<Setter Property="ItemContainerStyle"> 
    <Setter.Value> 
     <Style> 
     <Setter 
      Property="AutomationProperties.AutomationId" 
      Value="{Binding RelativeSource={RelativeSource Self}, Path=MagicPathForDisplayedText}"/> 
.... 

당신의 ItemsSource를 속성에 추가하지만 DisplayMemberPath가있는 Object 인 경우 내용은 해당 Object가됩니다.

도움이나 문제의 재 프레임을 보내 주셔서 감사합니다.

+0

당신이 '텍스트'속성을 사용하여 시도해 봤어에 결합 할 수있는 속성이? – Rachel

답변

1

이와 같은 문제를 처리하는 가장 쉬운 방법은 일반적으로 첨부 된 속성 및 동작입니다. 당신은 DisplayMemberPathDisplayText라는 두 개의 연결된 속성을 만들 수

는, 당신은 부모 ComboBoxDisplayMemberPath과에 DisplayMemberPath을 결합하는 PropertyChangedCallback 당신이 DisplayText에 대해 동일한 경로로 자신의 바인딩을 설정합니다. 그 후 당신은 당신이

<Style x:Key="ComboBoxStyle" TargetType="ComboBox"> 
    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <Style TargetType="ComboBoxItem"> 
       <Setter Property="behaviors:DisplayTextBehavior.DisplayMemberPath" 
         Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}, 
             Path=DisplayMemberPath}"/> 
       <Setter Property="AutomationProperties.AutomationId" 
         Value="{Binding RelativeSource={RelativeSource Self}, 
             Path=(behaviors:DisplayTextBehavior.DisplayText)}"/> 
      </Style> 
     </Setter.Value>      
    </Setter> 
</Style> 

DisplayTextBehavior

public class DisplayTextBehavior 
{ 
    public static DependencyProperty DisplayMemberPathProperty = 
     DependencyProperty.RegisterAttached("DisplayMemberPath", 
              typeof(string), 
              typeof(DisplayTextBehavior), 
              new FrameworkPropertyMetadata("", DisplayMemberPathChanged)); 
    public static string GetDisplayMemberPath(DependencyObject obj) 
    { 
     return (string)obj.GetValue(DisplayMemberPathProperty); 
    } 
    public static void SetDisplayMemberPath(DependencyObject obj, string value) 
    { 
     obj.SetValue(DisplayMemberPathProperty, value); 
    } 

    private static void DisplayMemberPathChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     ComboBoxItem comboBoxItem = sender as ComboBoxItem; 
     string displayMemberPath = GetDisplayMemberPath(comboBoxItem); 
     comboBoxItem.SetBinding(DisplayTextProperty, new Binding(displayMemberPath)); 
    } 

    public static DependencyProperty DisplayTextProperty = 
     DependencyProperty.RegisterAttached("DisplayText", 
              typeof(string), 
              typeof(DisplayTextBehavior), 
              new FrameworkPropertyMetadata("")); 
    public static string GetDisplayText(DependencyObject obj) 
    { 
     return (string)obj.GetValue(DisplayTextProperty); 
    } 
    public static void SetDisplayText(DependencyObject obj, string value) 
    { 
     obj.SetValue(DisplayTextProperty, value); 
    } 
} 
0

ViewModel의 중간 개체를 Combobox의 SelectedItem 속성에 바인딩 할 수 있습니다. 그런 다음 다른 디스플레이 항목을 해당 중간 개체에 바인딩하십시오. 그런 다음 항목을 선택하여 PropertyChanged 이벤트가 발생하면 이벤트 체인을 통해 디스플레이가 업데이트됩니다.

+0

내가 선택한 항목에 대해서만이 아니라 모든 항목에 이것을 원합니다 –

0

SelectedValuePath를 사용하고 있습니까? 그렇지 않은 경우 DisplayMemberPath와 동일하게 설정 한 다음 선택한 값을 SelectedValue로 사용할 수 있습니다.

+0

selecteditems에 대해서가 아니라 모든 항목에 대해 이것을 원합니다 –

+0

문제 문장은 "그"값 ""(단수)입니다. ComboBoxItem (다시 단수)에서 DisplayText로 요청합니다. 네가 원하는 걸 내게 전혀 분명하지 않다. – Paparazzi

관련 문제