首页 > 解决方案 > 将 listboxitem 内容绑定到列表框的依赖属性

问题描述

我正在尝试使用自定义 Checkedlistbox 项在 WPF 中创建一个自定义控件选中列表框

检查列表框项的 Xaml 代码:

<Style TargetType="{x:Type local:CheckedListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CheckedListBoxItem}">
                <CheckBox VerticalContentAlignment="Center" Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=Content}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

选中列表框的 Xaml 代码:

   <Style TargetType="{x:Type local:CheckedListBox}" BasedOn="{StaticResource {x:Type ListBox}}">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <local:CheckedListBoxItem Content="{Binding Name}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

选中的列表框类:

 public class CheckedListBox : ListBox
{
    static CheckedListBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckedListBox), new FrameworkPropertyMetadata(typeof(CheckedListBox)));
    }



    public string ItemBindingName
    {
        get { return (string)GetValue(ItemBindingNameProperty); }
        set { SetValue(ItemBindingNameProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ItemBindingName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ItemBindingNameProperty =
        DependencyProperty.Register("ItemBindingName", typeof(string), typeof(CheckedListBoxItem), new PropertyMetadata(""));


}

选中的 listboxitem 类:

public class CheckedListBoxItem : ListBoxItem
{
    static CheckedListBoxItem()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckedListBoxItem), new FrameworkPropertyMetadata(typeof(CheckedListBoxItem)));
    }



}

并且 iam 在带有以下 xaml 代码的窗口中使用它:

<GroupBox Grid.Column="2" Style="{StaticResource GroupBoxStyle}"  Header="Choose Categories :" >
        <local:CheckedListBox ItemsSource="{Binding AllCategories}" ItemBindingName="{Binding Name}"  />

    </GroupBox>

我想要的是以下内容:

为了使 customcheckedlistbox 如此通用,我不必在模板本身中输入我绑定到的属性的名称(即我想从 template 中删除 Content="{Binding Name}" 部分),而是我为checkedlistbox创建了一个名为“ItemBindingName”的依赖属性,我希望项目模板绑定其名称在“ItemBindingName”属性中的属性(即当我在checklistbox的声明中写ItemBindingName =“{Binding xxx}”时,模板自动绑定到“xxx”属性)

对不起,如果我的问题听起来不够理解,我真的很努力简化它,这是我能得到的最好的。

标签: wpfdata-bindinglistboxlistboxitemcheckedlistbox

解决方案


您的 CheckedListBox 类应覆盖以下方法。PrepareContainerForItemOverride它将建立内容绑定。

protected override DependencyObject GetContainerForItemOverride()
{
    return new CheckedListBoxItem();
}

protected override bool IsItemItsOwnContainerOverride(object item)
{
    return item is CheckedListBoxItem
        || base.IsItemItsOwnContainerOverride(item);
}

protected override void PrepareContainerForItemOverride(
    DependencyObject element, object item)
{
    if (!string.IsNullOrEmpty(ItemBindingName))
    {
        ((ContentControl)element).SetBinding(
            ContentControl.ContentProperty, new Binding(ItemBindingName));
    }
}

请注意,CheckedListBoxItem 的 ControlTemplate 中的内容绑定可能如下所示:

<CheckBox Content="{TemplateBinding Content}" ... />

推荐阅读