簡體   English   中英

WPF復選框與多行內容對齊到左上角

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

我有一個帶有HierachicalDatatemplates的WPF Treeview用於節點。 子節點包含用於選擇子節點的復選框。 到現在為止還挺好。

childnode的內容應該是一個復選框,其中包含多行內容的幾個Textbox,並根據viewmodel的一些圖片。

當我將內容放在復選框內時,該框始終顯示在內容的垂直中心。 但我希望復選框位於內容的左上角。

這是一個示例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但它沒有改變任何東西。

編輯:填充是一個解決方案,但不幸的是,無論哪個內容,復選框的位置應始終在左上角。 內容的行可能會有所不同......

也許somone已經解決了這個小問題。

由於選擇了節點,我希望內容在checkbo [x]中。

復選框控件使用ContentPresenter (帶復選框)主要用於處理基本文本方案。

因為選擇了節點。

這就是提供答案變得棘手的地方,因為現在要求必須在樹視圖的上下文(可以這么說)中工作。 只有你能說出這些要求......但......

選項

我看到你有這些選擇:

  1. 創建一個定制的復合控制, 擴展CheckBox控件真的 ,基本上呈現類似的工作,各地電網已如上圖所示(可在模板記住,您可以使用)。 請注意,您可以將圖像和內容綁定到自定義控件的內部,並顯示依賴項屬性,這些屬性隨后將被綁定。
  2. 在Blend中創建一個新的Checkbox樣式,並取出內容展示器,其中包含您需要的內容。 (我在下面的例子中這樣做了)。

沒有快速的方法可以做到這一點......但是可以使用自定義控件(我的主要建議)或模板更改。

新的復選框樣式

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM