创建绑定列表

时间:2012-07-09 13:45:42

标签: c# .net wpf xaml data-binding

我正在尝试在Binding的{​​{1}}上设置两个ListBox中的一个(基于ItemsSource组中的选择)。< / p>

我现在拥有的是:

RadioButton

然后理想情况下,我会根据用户的选择将其中一个设置为<ListBox> <ListBox.Resources> <Binding x:Key="AllBinding" Path="ItemsSource.AllLeaves" ElementName="EmployeeTree" /> <Binding x:Key="FolderBinding" Path="SelectedItem.Leaves" ElementName="EmployeeTree" /> </ListBox.Resources> </ListBox>

但我在ItemsSource s:

上都收到了此错误
  

无法在“DictionaryEntry”类型的“Value”属性上设置“绑定”。 '绑定'只能在DependencyObject的DependencyProperty上设置。

如何实施此要求?是否甚至可以定义Binding,以便我可以将它们从后面的代码中交换出来?

2 个答案:

答案 0 :(得分:2)

听起来您正试图根据某些TreeView.ItemsSource中的所选项目设置ListBox

如果是这种情况,您应该在DataTrigger上根据EmployeeTreeTreeView.ItemsSource

设置ListBox.SelectedItem ListBox.SelectedIndex
<Style x:Key="EmployeeTreeStyle" TargetType="{x:Type TreeView}">
    <!-- By default bind to MyListBox.SelectedItem.Leaves -->
    <Setter Property="ItemsSource" Value="{Binding ElementName=MyListBox, Path=SelectedItem.Leaves}" />
    <Style.Triggers>
        <!-- If SelectedIndex = 0, bind to AllLeaves instead -->
        <DataTrigger Binding="{Binding ElementName=MyListBox, Path=SelectedIndex}" Value="0">
            <Setter Property="ItemsSource" Value="{Binding AllLeaves}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

根据以下评论进行更新

<Style x:Key="EmployeeTreeStyle" TargetType="{x:Type TreeView}">
    <Style.Triggers>
        <!-- Set ItemsSource based on which RadioButton is Selected -->
        <DataTrigger Binding="{Binding ElementName=RadioButton1, Path=IsChecked}" Value="True">
            <Setter Property="ItemsSource" Value="{Binding AllLeaves}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=RadioButton2, Path=IsChecked}" Value="True">
            <Setter Property="ItemsSource" Value="{Binding ElementName=RadioButton2, Path=DataContext.Leaves}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

答案 1 :(得分:0)

我不完全确定你的要求......但我会在此之前给出。

如果你将DataContext设置为这样的东西;

 public class PresentationModel : INotifyPropertyChanged
{
    private object _userPropertyA;
    public object UserPropertyA
    {
        get { return _userPropertyA; }
        set
        {
            _userPropertyA = value;

            //Set the data source based on the value of the selected?
            DataSource = new List<object>();
        }
    }

    private object _userPropertyB;
    public object UserPropertyB
    {
        get { return _userPropertyB; }
        set
        {
            _userPropertyB = value;

            //Set the data source based on the value of the selected?
            DataSource = new List<object>();
        }
    }

    public List<object> DataSource { get; set; }

    #region Implementation of INotifyPropertyChanged

    public void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

然后将属性绑定到所需的控件

这将允许您根据用户传递的值更改数据源。

当然,你还需要实现INotifyPropertyChanged。

这有帮助吗?

欢呼声。 STE。