将listboxitem内容绑定到listbox的依赖项属性

问题描述 投票:0回答:1

我正在尝试使用自定义Checkedlistbox项在WPF中创建自定义控件的checkboxlistbox

checkedlistboxitem的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)));
    }



}

并使用以下xaml代码在窗口中使用它:

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

    </GroupBox>

我想要的是以下内容:

要使customcheckedlistbox如此通用,以至于我不必在模板本身中输入要绑定的属性的名称(即,我想从模板中删除Content =“ {Binding Name}”部分),相反,我为checkedlistbox创建了一个名为“ ItemBindingName”的依赖项属性,并且我希望项目模板绑定其名称位于“ ItemBindingName”属性中的属性(即,当我在声明中写入ItemBindingName =“ {Binding xxx}”时)被选中的列表框,模板会自动绑定到“ xxx”属性)

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

wpf data-binding listbox listboxitem checkedlistbox
1个回答
0
投票
protected override DependencyObject GetContainerForItemOverride() { return new CheckedListBoxItem(); } protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { if (!string.IsNullOrEmpty(ItemBindingName)) { ((ContentControl)element).SetBinding( ContentControl.ContentProperty, new Binding(ItemBindingName)); } } protected override bool IsItemItsOwnContainerOverride(object item) { return item is CheckedListBoxItem || base.IsItemItsOwnContainerOverride(item); }

注意,CheckedListBoxItem的ControlTemplate中的内容绑定看起来像这样:
<CheckBox Content="{TemplateBinding Content}" ... />
© www.soinside.com 2019 - 2024. All rights reserved.