0

我正在尝试在 a 上设置两个Bindings 中的一个(基于一组 s 中的选择ListBox)。ItemsSourceRadioButton

我现在拥有的是:

<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上都收到了这个错误Binding

不能在“DictionaryEntry”类型的“值”属性上设置“绑定”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。

我该如何实现这个要求?甚至可以定义Bindings 以便我可以将它们从后面的代码中交换出来吗?

4

2 回答 2

2

听起来您正在尝试TreeView.ItemsSource根据某些ListBox.

如果是这种情况,DataTrigger您应该EmployeeTree根据TreeView.ItemsSourceListBox.SelectedItemListBox.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>
于 2012-07-09T14:10:03.673 回答
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。

这有帮助吗?

干杯。圣。

于 2012-07-09T14:07:02.010 回答