如何在与复选框相同的级别上制作标签,但不在WPF中的标签点击上切换?

时间:2018-05-19 16:50:39

标签: wpf xaml checkbox

我想制作复选框列表,只有在直接点击复选框时才会切换。

不幸的是,如果我这样做:

                    <ListBox Name="LanguagesListBox">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <ListBoxItem>
                                    <StackPanel Orientation="Horizontal">
                                        <CheckBox/>
                                        <Label Content="{Binding InputLanguage.LayoutName}"/>
                                    </StackPanel>

                                </ListBoxItem>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

我得到的复选框与它的标签没有垂直对齐:

enter image description here

但如果我写

                  <ListBox Name="LanguagesListBox">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <ListBoxItem>
                                    <StackPanel Orientation="Horizontal">
                                        <CheckBox Content="{Binding InputLanguage.LayoutName}"/>

                                    </StackPanel>

                                </ListBoxItem>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

如果点击标签

,我会获得复选框切换

enter image description here

如何获得两者?

2 个答案:

答案 0 :(得分:0)

这是复选框行为,因此您无法更改它。 您必须创建一个用户控件来构建它。

答案 1 :(得分:0)

删除ListBoxItem,将Label替换为TextBlock并将其VerticalAlignment属性设置为Center

<ListBox Name="LanguagesListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox/>
                <TextBlock Text="{Binding InputLanguage.LayoutName}" VerticalAlignment="Center" Margin="2,-1,0,0"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您可以调整Margin的{​​{1}}属性,以进一步调整其位置。