Dinamik ListBox CheckBox İçeriği ayarlayın

0

Soru

Bir WPF kullanıcı denetimim var ListBox bu içerir CheckBoxes.

<UserControl x:Class="AC.CodA.WPF.UserControls.CheckedListBox"
             x:Name="Root"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:Background="White">
    <StackPanel>
        <ListBox BorderThickness="0"
                 ItemsSource="{Binding ElementName=Root, Path=ItemsSource}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Focusable"
                            Value="False" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsChecked}"
                              Content="{Binding Item}">
                    </CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Separator />
        <CheckBox x:Name="DeSelectAll"
                  Content="Select / deselect all"
                  Checked="DeSelectAll_Checked"
                  Unchecked="DeSelectAll_Unchecked" />
    </StackPanel>
</UserControl>

Bu kontrolü başka bir yerde kullanıyorum.:

<GroupBox Header="{Binding ElementName=Root, Path=Header, FallbackValue=Header}">
    <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" />
</GroupBox>

Bu mahalde Companies mülkiyet bir ObservableCollection<CheckedListBoxItem<Company>>.

CheckedListBoxItem ile bir sınıftır bool IsChecked mülkiyet ve jenerik T Item mülk.

Şimdi, T Item herhangi bir şey olabilir. Bu noktada bu özellik şu şekilde ayarlandı: Content of the CheckBox ancak çoğu zaman bu, aşağıdaki gibi belirsiz bir şeyle sonuçlanır AC.CodA.Scripting.Shared.Company.

Bir şekilde ayarlamak için CheckBox Content kullanıcı kontrolü dışında mı? Böyle bir şeyle CheckBoxContent:

<GroupBox Header="{Binding ElementName=Root, Path=Header, FallbackValue=Header}">
    <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Item.Name}" />
</GroupBox>

ve nesnemin içinde bu:

<CheckBox IsChecked="{Binding IsChecked}"
          Content="{Binding CheckBoxContent}">
</CheckBox>
binding c# wpf
2021-11-23 14:51:41
1

En iyi cevabı

1

Bir ekle CheckBoxContent bağımlılık özelliği UserControl's arka planda kod dosyası:

public partial class CheckedListBox : UserControl
{
    ...

    public static readonly DependencyProperty CheckBoxContentProperty =
        DependencyProperty.Register(nameof(CheckBoxContent), typeof(object),
            typeof(CheckedListBox));

    public object CheckBoxContent
    {
        get { return GetValue(CheckBoxContentProperty); }
        set { SetValue(CheckBoxContentProperty, value); }
    }
}

...ve XAML işaretlemenizde ona bağlanın:

<CheckBox IsChecked="{Binding IsChecked}"
          Content="{Binding CheckBoxContent,
              RelativeSource={RelativeSource AncestorType=UserControl}}">
</CheckBox>
2021-11-24 15:02:23

Cevap için Tnx. Bunu önerdiğin gibi yaptım ama şimdi bunu nasıl doldurabilirim CheckBoxContent kullanırken CheckedListBox? Bir ders al Company örneğin bir özellik ile Name. Aşağıdakileri denedim ancak içerik boş: <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Name}" />.
Krowi

Özelliği sabit bir dize değerine ayarlarsanız, örneğin çalışır mı: CheckBoxContent="test"? Sonra bağlama Name başarısız olmak.
mm8

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................