Impostare la Casella di controllo ListBox Contenuti in modo dinamico

0

Domanda

Ho un controllo utente WPF che è un ListBox che contiene 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>

Sto usando questo controllo in un altro luogo:

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

In questo luogo, il Companies la struttura è un ObservableCollection<CheckedListBoxItem<Company>>.

CheckedListBoxItem è una classe con bool IsChecked proprietà e generico T Item proprietà.

Ora, T Item può essere qualsiasi cosa. A questo punto, ho questa proprietà è impostata come Content del CheckBox ma la maggior parte delle volte questo si traduce in qualcosa di ambiguo come AC.CodA.Scripting.Shared.Company.

C'è un modo per impostare il CheckBox Content al di fuori del controllo utente? Qualcosa di simile a questo con CheckBoxContent:

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

e dentro il mio oggetto di questo:

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

Migliore risposta

1

Aggiungere un CheckBoxContent proprietà di dipendenza per il tuo UserControl's file di code-behind:

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

...e si legano ad essa nel markup XAML:

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

Tnx per la risposta. Ho fatto come hai suggerito ma come faccio ora a riempire questo CheckBoxContent quando si utilizza il CheckedListBox? Prendere una classe Company per esempio con una proprietà Name. Ho provato ma il contenuto è vuoto: <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Name}" />.
Krowi

Funziona se si imposta la proprietà per un valore di stringa costante e.g: CheckBoxContent="test"? L'associazione per Name non riesce.
mm8

In altre lingue

Questa pagina è in altre lingue

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