C# WPF复选框与多行内容左上对齐

C# WPF复选框与多行内容左上对齐,c#,wpf,checkbox,C#,Wpf,Checkbox,我有一个WPF树视图,其中包含节点的层次数据模板。 子节点包含用于选择子节点的复选框。 到目前为止还不错 childnode的内容应该是一个复选框,其中包含多行内容、多个文本框以及一些图片 当我将内容放入复选框时,该框始终显示在内容的垂直中心。 但我希望复选框位于内容的左上角 以下是WPF代码示例: <Window x:Class="Spielwiese.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pres

我有一个WPF树视图,其中包含节点的层次数据模板。 子节点包含用于选择子节点的复选框。 到目前为止还不错

childnode的内容应该是一个复选框,其中包含多行内容、多个文本框以及一些图片

当我将内容放入复选框时,该框始终显示在内容的垂直中心。 但我希望复选框位于内容的左上角

以下是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>

由于节点的选择,我更希望内容位于checkbock中。 我已经尝试为checkbock设置verticalcontentalignment,但没有任何改变

编辑: 填充是一种解决方案,但不幸的是,无论内容是什么,复选框的位置都应该始终位于左上角。内容的行可能会有所不同

也许有人已经解决了这个小问题

我希望内容在checkbo[x]中,因为 节点的选择

复选框控件使用一个
ContentPresenter
,它(与复选框一起)主要用于基本文本场景

因为节点的选择

这就是提供答案变得棘手的地方,因为现在需求必须在树视图的上下文(可以这么说)中工作。只有你能满足这些要求…但是

选择权 在我看来,您有以下选择:

  • 创建一个定制的复合控件(可以在模板中使用),一个扩展的checkbox控件,它基本上呈现为您上面展示的网格周围的工作。请注意,然后可以将图像和内容绑定到自定义控件的内部,并公开依赖项属性,这些属性随后将被绑定
  • 在Blend中创建一个新的
    复选框
    样式,并取出内容演示器,其中包含您需要包含的内容。(我在下面的例子中这样做了)
  • 没有快速的方法可以做到这一点…但是可以使用自定义控件(我的主要建议)或模板更改

    新复选框样式 原始ContentPresenter已被删除,并替换为一个网格以容纳复选框和文本。下面的样式创建了以下内容:

    <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>
    

    
    

    共享您的TreeView模板。TreeView模板与此问题无关,我在上面的XAML代码片段中也遇到了此问题。模板的代码太多。自定义控件上的依赖项属性很容易。我使用这些代码片段(适用于所有风格的Xaml WPF/Windows Phone)。通过这种方式,您可以将所有内容绑定到依赖项属性,就像来自Microsoft的控件一样。这看起来不错。因此,我将尝试根据自己的需要创建一个自定义的复选框usercontrol。这不是我所期望的简单方法,而是一个解决方案。我试着玩一些填充,但我发现没有办法根据内容的大小调整填充。。。到目前为止,这可能是我的解决方案。自定义控件上的依赖属性很容易。我使用这些代码片段(适用于所有风格的Xaml WPF/Windows Phone)。通过这种方式,您可以将所有内容绑定到依赖项属性,就像来自Microsoft的控件一样。