2011-08-18 47 views
1

我有一个组合框,它有一个对象列表的ItemsSource。所以DisplayMemberPath被设置为该对象的特定属性。当然这意味着在ComboBoxItem中显示正确的值。Access ComboBoxItem DisplayValue

我的问题是,我希望能够得到由DisplayMemberPath在XAML中返回的“Value”,以便我可以将它绑定到其他位置。即我想在ComboBoxItem上有一个“DisplayText”属性。

当然,我没有这个,所以,有没有人知道一种方法来获得这个值,而无需遍历到ComboBoxItem的模板寻找ContentHost?

如果你有兴趣在我的具体使用的这个,我试图做到这一点的组合框的样式:

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

当然,如果你只是结合Path=Content工作得很好,你的ItemsSource属性,但是当它是具有DisplayMemberPath的Object时,Content将是该Object。

感谢您的任何帮助或重新框架的问题。

+0

你尝试使用'Text'属性的属性? – Rachel

回答

1

处理这种问题最简单的方法通常是附加属性和行为。

您可以创建两个附加属性称为DisplayMemberPathDisplayText,然后绑定DisplayMemberPathComboBoxDisplayMemberPath并在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

我想要这个所有项目,而不仅仅是选定的项目 –

+0

你问题陈述是“得到那个”价值“”(单数)。您在ComboBoxItem上请求DisplayText(再次单数)。我一点也不清楚你想要什么。 – Paparazzi