I have a WPF UserControl
in my Excel add-in project. This control has a ListBox
and a StackPanel
with two buttons. ListBox
consists of items, each of them corresponding to an excel workbook sheet. Also we have two commands to hide/show sheets then CheckBox
es are checked/uncheked. This part is fine.
The question is how to bind these existing commands to buttons in the stackpanel below. It is necessary that we can multiselect several items in the ListBox then click the button and get the result.
XAML code:
<UserControl x:Class="ListsAddin.ListManagementControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}">
<Trigger Property="IsChecked" Value="True">
<Setter Property="Command" Value="{Binding HideSheetCommand}"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Command" Value="{Binding ShowSheetCommand}"/>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<StackPanel >
<ListBox Name="lb" ItemsSource="{Binding lst}" Margin="0,0,0,7" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=SheetName}"/>
<CheckBox IsChecked="{Binding Path=IsHidden}"
Style="{StaticResource CheckBoxStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel>
<Button Content="HideSheet" Command="{ ?? }"/>
<Button Content="ShowSheet" Command="{ ?? }" />
</StackPanel>
</StackPanel>
</UserControl>