【问题标题】:xaml bind value of listbox item to whether is列表框项的 xaml 绑定值是否为
【发布时间】:2013-02-14 21:11:41
【问题描述】:

我正在 VS 2012 中构建一个 WPF 应用程序,它使用 Code First Entity Framework 以及存储库和 MVVM 模式。

我有两个类:EntityType 和 LicenseType。

EntityType 由以下内容指定:

    private int _id;
    private string _name;
    private string _description;
    private List<LicenseType> _licenseTypes = new List<LicenseType>();

LicenseType 声明为:

    private int _id;
    private string _name;
    private string _description;

并且为这些私有字段声明了公共属性。

我将 ObservableCollection 绑定到 CheckedListBox(Syncfusion 产品)。

没问题。

在它下面,我将另一个 CheckedListBox 绑定到 ObservableCollection。

再次,没问题。

我不能做的是根据特定的 LicenseType 是否在 EntityType.LicenseTypes 的集合中,将 LicenseType 列表框中 CheckListBoxItem 的 IsSelected 属性设置为 true(从而“选中”复选框)。

这是我的 CheckedListBox 的代码:

         <syncfusion:CheckListBox x:Name="lstAllLicenseTypes" ItemsSource="{Binding   AllLicenseTypes}" DisplayMemberPath="Name" >
           <syncfusion:CheckListBox.ItemContainerStyle>
                <Style TargetType="syncfusion:CheckListBoxItem">
                    <Setter Property="IsSelected" Value="{Binding ????}"/>
                </Style>
            </syncfusion:CheckListBox.ItemContainerStyle>
        </syncfusion:CheckListBox>

我尝试了两种方法,第一种是遍历每个 LicenseType 列表框项,检查是否与 EntityType 的 LicenseType 集合有关联,并尝试设置绑定到 XAML 的 Property 值:

private void ListEntityTypes_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { var thisEntity = (EntityType) listEntityTypes.SelectedItems[0];

        foreach (object t in lstAllLicenseTypes.Items)
        {
            bool hasAssociation = _vmEntityTypes.EntityHasAssociationWithThisLicenseType(thisEntity,(LicenseType) t);
            if (hasAssociation)
            {
                _vmEntityTypes.CurrentEntityIsAssociatedWithCurrentLicenseType = true;
            }
            else
            {
                _vmEntityTypes.CurrentEntityIsAssociatedWithCurrentLicenseType = false;
            }
        }

    }

这里的目的是绑定到属性“CurrentEntityIsAssociatedWithCurrentLicenseType”。这没有用。我认为它不起作用,因为对于 CheckedListBox 中的每个项目,属性值都已更新,因此最后它是 false,因此没有检查任何项目。

另一种方法与上述类似,但我试图手动“检查”列表框项,但我永远无法将 LicenseType 转换回 CheckListItem。

一般来说,我的弱项是处理与其他数据集合相关的记录集合,并构建一个允许用户清楚地添加和关联不同表的界面。

任何帮助都会很棒,

谢谢! 保罗

【问题讨论】:

    标签: wpf xaml syncfusion


    【解决方案1】:

    使用转换器。将 IsChecked 绑定到包装列表 (EntityType.LicenseTypes) 的属性,然后使用返回布尔值的 IValueConverter。将 CheckedListBoxItem 的 LicenseType 作为 ConverterParameter 传递。

    在 EntityType 类上实现 IPropertyNofity。

    当 EntityType.LicenseTypes 列表发生变化时,对被包装的属性使用 PropertyNotify,isChecked 会更新。

    伪代码:

    public class EntityType : INotifyPropertyChanged
    {
        ...
        public List<LicenseType> LicenseTypes
        { 
            get { return _licenseTypes; }
            set
            {
                _licenseTypes = value;
                OnNotifyPropertyChanged("LicenseTypes");
            }
        }
    
    }
    
    public class ListToCheckedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter ...
        {
            var list = (List<LicenseType>)value;
            var type = (LicenseType)parameter;
            return list.Contains(type);
        }
        ...
    }
    

    XAML:

        <syncfusion:CheckListBox x:Name="lstAllLicenseTypes" ItemsSource="{Binding   AllLicenseTypes}" DisplayMemberPath="Name" >
           <syncfusion:CheckListBox.ItemContainerStyle>
                <Style TargetType="syncfusion:CheckListBoxItem">
                    <Setter Property="IsSelected" 
                        Value="{Binding Path=LicenseTypes, 
                                Converter = ListToCheckedConverter,
                                ConverterParameter = Header}"/>
                </Style>
            </syncfusion:CheckListBox.ItemContainerStyle>
        </syncfusion:CheckListBox>
    

    【讨论】:

    • 另外,您可以将 IsSelected 绑定到 CheckedListBoxItem 的 Header 本身,并传入一个 EntityList,但是如果 EntityList 更新,您将失去自动更新的能力。
    • 或者,您可以使用多重绑定并绑定 Header 和 EntityList,如果其中一个更新。只是一些想法。
    • 我实现了您在第一个解决方案中给出的更改,但是,转换器功能没有触发。这是代码:
    • public class ListToCheckedConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfoculture) { var list = (ObservableCollection)value; var type = (LicenseType) 参数;返回列表。包含(类型); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo 文化) { throw new NotImplementedException(); } }
    • IValueConverter 函数 Convert 未触发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多