将复选框添加到wpf列表框以进行多项选择

时间:2011-02-25 16:48:34

标签: c# wpf datatemplate listboxitem

  <StackPanel Grid.Column="0" >
            <Button Name="buttonEditListBoxItem" Content="Edit" Click="buttonEditListBoxItem_Click"></Button>
            <ListBox  Name="ListBoxTriggers" 
                 SelectedValuePath="TriggerId" 
                 IsSynchronizedWithCurrentItem="True"
                 SelectionChanged="Triggers_SelectionChanged"
                 ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                 HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate><Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="AUTO"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <CheckBox Grid.Column="0" VerticalAlignment="Center" x:Name="checkBoxTriggers" ></CheckBox>
                            <Button Grid.Column="1" Style="{StaticResource GlassButton}" 
                            Uid="{Binding Path=TriggerId}" 
                            Margin="5"
                           x:Name="ButtonTrigger"
                           GotFocus="ButtonTrigger_GotFocus"
                          >
                                <Button.Content>
                                    <TextBlock Foreground="White" TextAlignment="Justify"
                                       TextWrapping="Wrap" Margin="6"   Text="{Binding Path=Name}"/>
                                </Button.Content>
                            </Button>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate></ListBox>

            </StackPanel>

我想创建一个包含数据模板的列表框,其中包含一个按钮和一个复选框。我想要做的是当我点击编辑按钮时,这些复选框应该可见,当我再次点击它时,我想让这些复选框不可见/折叠

答:我现在已将代码更改为:`         

</Window.Resources>  <StackPanel Grid.Column="0" >
            <ToggleButton Name="buttonEditListBoxItem" 
                          Content="Edit" 
                          IsChecked="False"
                          Click="buttonEditListBoxItem_Click"></ToggleButton>
            <ListBox  Name="ListBoxTriggers" 
                 SelectedValuePath="TriggerId" 
                 IsSynchronizedWithCurrentItem="True"
                 SelectionChanged="Triggers_SelectionChanged"
                 ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                 HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>  <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="AUTO"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <CheckBox Visibility="{Binding ElementName=buttonEditListBoxItem, Path=IsChecked, Converter={StaticResource visibilityConverter}}" Grid.Column="0" VerticalAlignment="Center" x:Name="checkBoxTriggers" ></CheckBox>
                            <Button Grid.Column="1" Style="{StaticResource GlassButton}" 
                            Uid="{Binding Path=TriggerId}" 
                            Margin="5"
                           x:Name="ButtonTrigger"
                           GotFocus="ButtonTrigger_GotFocus"
                          >
                                <Button.Content>
                                    <TextBlock Foreground="White" TextAlignment="Justify"
                                       TextWrapping="Wrap" Margin="6"   Text="{Binding Path=Name}"/>
                                </Button.Content>
                            </Button>
                        </Grid>
                    </DataTemplate> </ListBox.ItemTemplate>
        </ListBox>` `public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((Boolean)value) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (((Visibility)value) == Visibility.Visible);
    }
}`

2 个答案:

答案 0 :(得分:1)

如何使用ToggleButton代替Button代替VisibilityConverter

<ListBox  Name="ListBoxTriggers"
            SelectedValuePath="TriggerId"
            IsSynchronizedWithCurrentItem="True"
            SelectionChanged="Triggers_SelectionChanged"
            ScrollViewer.HorizontalScrollBarVisibility="Auto"
            HorizontalContentAlignment="Stretch">
    <ListBox.Resources>
        <local:VisibilityConverter x:Key="VisibilityConverter" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="AUTO" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <CheckBox Grid.Column="0"
                            VerticalAlignment="Center"
                            x:Name="checkBoxTriggers"
                            Visibility="{Binding ElementName=ButtonTrigger, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"></CheckBox>
                <ToggleButton Grid.Column="1"
                                Uid="{Binding Path=TriggerId}"
                                Margin="5"
                                x:Name="ButtonTrigger"
                                GotFocus="ButtonTrigger_GotFocus">
                    <ToggleButton.Content>
                        <TextBlock Foreground="White"
                                    TextAlignment="Justify"
                                    TextWrapping="Wrap"
                                    Margin="6"
                                    Text="{Binding Path=Name}" />
                    </ToggleButton.Content>
                </ToggleButton>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

VisibilityConverter:

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((Boolean)value) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (((Visibility)value) == Visibility.Visible);
    }
}

答案 1 :(得分:1)

实现这一目标的最简单方法是使用togglebutton

示例:

<ToggleButton  Name="Toggler">
</ToggleButton>
<StackPanel Visibility="{Binding ElementName=Toggler, Path=Checked, Converter={StaticResource booleanToVisibilityConverter}}">
<!-- in here you can place the checkboxes-->
</StackPanel>

你只需要创建一个valueconverter,它从boolean转换为枚举Visibility

另一种可能性是将“Popup”元素与ToggleButton结合使用(它会给出一个小助手的感觉,弹出来编辑项目,如果设置StaysOpen =“False”,则会在失去焦点时立即关闭) 如果您使用此方法,则无需按下编辑按钮以隐藏复选框