简体   繁体   English

WPF复选框与多行内容对齐到左上角

[英]WPF Checkbox align to top left with multiline content

I've got a WPF Treeview with HierachicalDatatemplates for the nodes. 我有一个带有HierachicalDatatemplates的WPF Treeview用于节点。 The Child nodes are containing checkboxes for selecting the children. 子节点包含用于选择子节点的复选框。 So far so good. 到现在为止还挺好。

The content of the childnode should be a checkbox with multiline content several Textboxes and according to the viewmodel some pics. childnode的内容应该是一个复选框,其中包含多行内容的几个Textbox,并根据viewmodel的一些图片。

When I put the content inside the checkbox, the box is always displayed in the vertical center of the content. 当我将内容放在复选框内时,该框始终显示在内容的垂直中心。 But I want the checkbox to be on the top left of the content. 但我希望复选框位于内容的左上角。

Here is an example WPF code: 这是一个示例WPF代码:

<Window x:Class="Spielwiese.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <!--THIS IS WHAT I LIKE TO USE-->
    <CheckBox Grid.Column="0" Margin="5">
        <StackPanel>
            <TextBlock Text="Some text" />
            <TextBlock Text="Some more text" />
            <TextBlock Text="a lot of more text" />
        </StackPanel>
    </CheckBox>       


    <!--THIS IS MY ACTUAL WORKAROUND-->
    <Grid Grid.Column="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
        </Grid.ColumnDefinitions>
        <CheckBox Grid.Column="0" Margin="0,3,5,0" />
        <StackPanel Grid.Column="1">
            <TextBlock Text="Some text" />
            <TextBlock Text="Some more text" />
            <TextBlock Text="a lot of more text" />
        </StackPanel>
    </Grid>
</Grid>

I would prefer the content to be inside the checkbock because of the selection of the nodes. 由于选择了节点,我宁愿将内容放在checkbock中。 I already tried to set verticalcontentalignment for the checkbock but it changed nothing. 我已经尝试为checkbock设置verticalcontentalignment但它没有改变任何东西。

EDIT: Padding is a solution but unfortunately the position of the checkbox should be always on top left no matter which content. 编辑:填充是一个解决方案,但不幸的是,无论哪个内容,复选框的位置应始终在左上角。 The lines of the content may vary... 内容的行可能会有所不同......

Maybe somone has already solution for this little problem. 也许somone已经解决了这个小问题。

I would prefer the content to be inside the checkbo[x] because of the selection of the nodes. 由于选择了节点,我希望内容在checkbo [x]中。

The checkbox control uses a ContentPresenter which (with the checkbox) is primarily designed to work with basic text scenarios. 复选框控件使用ContentPresenter (带复选框)主要用于处理基本文本方案。

because of the selection of the nodes. 因为选择了节点。

This is where providing an answer gets tricky because now the requirement has to work within the context (so to speak) of a treeview. 这就是提供答案变得棘手的地方,因为现在要求必须在树视图的上下文(可以这么说)中工作。 Only you can speak to those requirements...but... 只有你能说出这些要求......但......

Options 选项

As I see it you have these options: 我看到你有这些选择:

  1. Create a custom composite control (can be used in a template mind you), an extended checkbox control really , which basically presents like the work around grid you have shown above. 创建一个定制的复合控制, 扩展CheckBox控件真的 ,基本上呈现类似的工作,各地电网已如上图所示(可在模板记住,您可以使用)。 Note that you can then have the images and content bound to the internals of the custom control with dependency properties exposed which will be subsequently bound. 请注意,您可以将图像和内容绑定到自定义控件的内部,并显示依赖项属性,这些属性随后将被绑定。
  2. Create a new Checkbox style in Blend and take out the content presenter with the content you need included within it. 在Blend中创建一个新的Checkbox样式,并取出内容展示器,其中包含您需要的内容。 (I did this in the example below). (我在下面的例子中这样做了)。

There is no quick way of doing this...but either by a custom control (my primary advice to do) or a template change can be used. 没有快速的方法可以做到这一点......但是可以使用自定义控件(我的主要建议)或模板更改。

New Check Box Style 新的复选框样式

The original ContentPresenter has been removed and replaced with a grid to hold the checkbox and text. 原始ContentPresenter已被删除并替换为网格以保存复选框和文本。 The below style creates this: 以下样式创建了这个:

复选框样式结果

<Style x:Key="OmegaCheckBoxStyle" TargetType="{x:Type CheckBox}">
<Setter Property="Foreground" 
        Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="{StaticResource CheckBoxFillNormal}"/>
<Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource EmptyCheckBoxFocusVisual}"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type CheckBox}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="4" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>

            </Grid.RowDefinitions>
                <BulletDecorator Background="Transparent" 
                                 SnapsToDevicePixels="true"  
                                 VerticalAlignment="Bottom">
                    <BulletDecorator.Bullet>
                         <Themes:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" 
                                            Background="{TemplateBinding Background}" 
                                            IsChecked="{TemplateBinding IsChecked}" 
                                    RenderMouseOver="{TemplateBinding IsMouseOver}" 
                                            RenderPressed="{TemplateBinding IsPressed}"
                                                />
                    </BulletDecorator.Bullet>
                </BulletDecorator>

                <TextBlock Text="Some text"  Grid.Row="0" Grid.Column="2"/>
                <TextBlock Text="Some more text" Grid.Row="1" Grid.Column="2"/>
                <TextBlock Text="a lot of more text" Grid.Row="2" Grid.Column="2"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="HasContent" Value="true">
                    <Setter Property="FocusVisualStyle" 
                            Value="{StaticResource CheckRadioFocusVisual}"/>
                    <Setter Property="Padding" Value="4,0,0,0"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" 
                            Value="{DynamicResource {x:Static 
                                                     SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>            
</Setter>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM