【问题标题】:Creating a list of Bindings创建绑定列表
【发布时间】:2012-07-09 13:45:42
【问题描述】:

我正在尝试在ListBoxItemsSource 上设置两个Bindings 之一(基于一组RadioButtons 中的选择)。

我现在拥有的是:

<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

但我在Bindings 上都收到此错误:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    如果是这种情况,您应该在EmployeeTree 上写一个DataTrigger,根据ListBox.SelectedItemListBox.SelectedIndex 设置TreeView.ItemsSource

    <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>
    

    根据下面的 cmets 更新

    <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>
    

    【讨论】:

    • 我实际上并没有使用 ListBox。我正在使用单选按钮。这个想法也适用于 RadioButtons 吗?
    • 您的意思是 SelectedIndex Value=-1,而不是 =0。 0 是选择的第一个条目,-1 是没有选择条目...
    • @KendallFrey 是的,同样的概念也适用于RadioButtons
    • 我怎样才能让它与 RadioButtons 一起工作?使用 DataTrigger 检查选中的 RadioButton?
    • @eFloh 我假设第一个条目是“所有项目”并且起始 SelectedIndex 设置为 0,因为听起来应该允许用户导航回所有项目选择.
    【解决方案2】:

    我不完全确定您的要求...但我会先给出。

    如果您将 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。

    这有帮助吗?

    干杯。 步骤。

    【讨论】:

    • 要使 INotifyPropertyChanged 工作,您必须在 DataSource = new List&lt;object&gt;(); 之后调用 OnPropertyChanged("DataSource")
    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2018-09-27
    相关资源
    最近更新 更多