如何在WPF ItemsControl子项中设置数据绑定以在父数据上下文中使用属性?

时间:2010-07-15 15:01:43

标签: wpf wpf-controls binding

我有一个带有RadioButtons集合的WPF ListView。我想将子控件的GroupName设置为绑定到父数据上下文的属性。目前我通过复制每个孩子的数据上下文中的属性来做到这一点,但这可能不对。

我的XAML:

 <ListView ItemsSource="{Binding OptionItems}" >
     <ListView.ItemTemplate>
         <DataTemplate DataType="{x:Type Logging:FilterOptionsRadioListViewModel}">
            <RadioButton GroupName="{Binding GroupName}" Content="{Binding Option.Value}" Tag="{Binding Option.Key}" IsChecked="{Binding IsChecked}" Command="Logging:FilterOptionsRadioListViewModel.CheckedChangedCommand" />
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

GroupName是父视图模型上的属性。我目前将它传递给子构造函数中的子视图模型(它也是一个属性):

var item = new FilterOptionsRadioListItemViewModel(option, this.GroupName);

这样做的正确方法是什么?

3 个答案:

答案 0 :(得分:1)

 <ListView ItemsSource="{Binding OptionItems}" >
     <ListView.ItemTemplate>
         <DataTemplate DataType="{x:Type Logging:FilterOptionsRadioListViewModel}">
            <RadioButton GroupName="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.GroupName}" Content="{Binding Option.Value}" Tag="{Binding Option.Key}" IsChecked="{Binding IsChecked}" Command="Logging:FilterOptionsRadioListViewModel.CheckedChangedCommand" />
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

答案 1 :(得分:0)

您当然可以这样做,但如果您不想将GroupName复制到每个子选项实例,您可以改为执行以下操作

// I'm imagining your viewmodels look something like this
public class ParentViewModel
{
    private ObservableCollection<Option> m_OptionItems;
    public ObservableCollection<Option> OptionItems
    {
        get { return m_OptionItems; }
        set { m_OptionItems = value; }
    }

    private string m_ParentGroupName;
    public string ParentGroupName
    {
        get { return m_ParentGroupName; }
        set
        {
            m_ParentGroupName = value;
        }
    }


}

public class Option
{
    private string m_Value;
    public string Value
    {
        get { return m_Value; }
        set
        {
            m_Value = value;
        }
    }



}

然后,您可以使用相对绑定,以便查找控制树以查找正确的datacontext,因此您不必将ParentGroupName复制到每个子选项实例。

                                              

答案 2 :(得分:0)

如果您命名窗口,则可以遍历根DataContext。

<Window x:Class="MyNamespace.MainWindow"
        Name="Window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="500" Width="725">

<ListView ItemsSource="{Binding OptionItems}" >
     <ListView.ItemTemplate>
         <DataTemplate DataType="{x:Type Logging:FilterOptionsRadioListViewModel}">
            <RadioButton GroupName="{Binding ElementName=Window, Path=DataContext.ParentGroupName}" Content="{Binding Option.Value}" Tag="{Binding Option.Key}" IsChecked="{Binding IsChecked}" Command="Logging:FilterOptionsRadioListViewModel.CheckedChangedCommand" />
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>