C# WPF复选框与多行内容左上对齐
我有一个WPF树视图,其中包含节点的层次数据模板。 子节点包含用于选择子节点的复选框。 到目前为止还不错 childnode的内容应该是一个复选框,其中包含多行内容、多个文本框以及一些图片 当我将内容放入复选框时,该框始终显示在内容的垂直中心。 但我希望复选框位于内容的左上角 以下是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
<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
,它(与复选框一起)主要用于基本文本场景
因为节点的选择
这就是提供答案变得棘手的地方,因为现在需求必须在树视图的上下文(可以这么说)中工作。只有你能满足这些要求…但是
选择权
在我看来,您有以下选择:
复选框
样式,并取出内容演示器,其中包含您需要包含的内容。(我在下面的例子中这样做了)<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的控件一样。