0

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 CheckBoxes 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>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Lebedev Alexei
  • 121
  • 1
  • 1
  • Unless the command is on the item, you'll want to bind to the parent with RelativeSource http://stackoverflow.com/questions/2439593/bind-to-a-property-of-a-parent-element-in-wpf – kenny Dec 20 '12 at 12:37
  • Yes, you are right. But how to bind only selected items? Can you provide a code exapmle? Thanks in advance. – Lebedev Alexei Dec 21 '12 at 06:48
  • I don't think you can bind only to selected commands, but you can have the command's action only use selected items. – kenny Dec 21 '12 at 08:59

0 Answers0