Krowi
Krowi

Reputation: 1615

Set ListBox CheckBox Content dynamically

I have a WPF user control which is a ListBox that contains 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>

I'm using this control at another place:

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

At this place, the Companies property is an ObservableCollection<CheckedListBoxItem<Company>>.

CheckedListBoxItem is a class with bool IsChecked property and generic T Item property.

Now, T Item can be anything. At this point I have this property set as the Content of the CheckBox but most times this results in something ambiguous like AC.CodA.Scripting.Shared.Company.

Is there a way to set the CheckBox Content outside of the user control? Something like this with CheckBoxContent:

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

and inside my object this:

<CheckBox IsChecked="{Binding IsChecked}"
          Content="{Binding CheckBoxContent}">
</CheckBox>

Upvotes: 0

Views: 184

Answers (1)

mm8
mm8

Reputation: 169228

Add a CheckBoxContent dependency property to your UserControl's code-behind file:

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

...and bind to it in your XAML markup:

<CheckBox IsChecked="{Binding IsChecked}"
          Content="{Binding CheckBoxContent,
              RelativeSource={RelativeSource AncestorType=UserControl}}">
</CheckBox>

Upvotes: 1

Related Questions