【问题标题】:Not able to display checkboxes for ListViewItems in WPF无法在 WPF 中显示 ListViewItems 的复选框
【发布时间】:2016-09-21 01:19:54
【问题描述】:

我想在 ListView 中的文本旁边显示复选框。我的 WPF 窗口中有多个 ListView,因此我想将此行为指定为资源中的样式。但是不显示复选框,只显示文本。因此,请在我的代码下方找到并提出修改建议。

<Window.Resources>
    <Style TargetType="ListView"  x:Key="ListViewTemplate">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="BorderBrush" Value="Transparent"></Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Content=""></CheckBox>
                        <Separator Width="5"></Separator>
                        <TextBlock Text="{Binding Text}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListView Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="8" Grid.RowSpan="5" x:Name="listViewDocTypes" Style="{DynamicResource ListViewTemplate}"    >
    </ListView>
</Grid>

【问题讨论】:

    标签: c# wpf listview checkbox


    【解决方案1】:

    Checkbox 提供TextWidth。如果不设置Text 属性,如果您不指定其Width,长度将自动为“0”。

    <CheckBox Content="xyz" Width="1234"/>
    

    【讨论】:

    • 您是否尝试将Stack PanelOrientation 设置为Vertical
    • 问题在于绑定,我的错
    【解决方案2】:

    问题是我以错误的方式绑定数据。我使用的是以下代码。

     ListViewItem item = new ListViewItem();
                            item.Tag = docType;
                            item.Text = docType.Text;
                            listViewDocTypes.Items.Add(item);
    

    正确的绑定方式是

    listViewDocTypes.ItemsSource = docTypes;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2019-07-10
      相关资源
      最近更新 更多