wpf新手-如何布局变量属性集,使其与固定属性显示在相同的列中

wpf新手-如何布局变量属性集,使其与固定属性显示在相同的列中,wpf,layout,grid,Wpf,Layout,Grid,我是wpf的新手&喜欢它。然而,我有一个布局问题,我希望有人能帮助我。我需要建立一个个人属性编辑器。这些由两个固定属性组成——名字和姓氏,加上年龄、性别等其他属性的可变范围 我已经建立了一个对话框,其中包括两个固定属性的文本框和一个可变属性的列表框 <Grid Name="mainGrid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <Colum

我是wpf的新手&喜欢它。然而,我有一个布局问题,我希望有人能帮助我。我需要建立一个个人属性编辑器。这些由两个固定属性组成——名字和姓氏,加上年龄、性别等其他属性的可变范围

我已经建立了一个对话框,其中包括两个固定属性的文本框和一个可变属性的列表框

<Grid Name="mainGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Label Grid.Column="0" Grid.Row="0" VerticalAlignment="Center">First Name:</Label>  
    <TextBox Name="tbFirstName" Grid.Column="2" Grid.Row="0" MinWidth="100" Margin="5" Text="{Binding Path=FirstName}"/>

    <Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center">Last Name:</Label>
    <TextBox Name="tbLastName" Grid.Column="2" Grid.Row="1" MinWidth="100" Margin="5" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}"/>     

    <ListBox Name="lstAttributes" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding Path=Attributes, UpdateSourceTrigger=PropertyChanged}"/>

    <StackPanel Orientation="Horizontal" Grid.Row="3" Grid.ColumnSpan="3" HorizontalAlignment="Right">
        <Button Name="btnOk" IsDefault="True" Click="btnOk_Click" Grid.Column="0" Grid.Row="2" MinWidth="60" Margin="5">Ok</Button>
        <Button Name="btnCancel" IsCancel="True" Grid.Column="0" Grid.Row="2" MinWidth="60" Margin="5">Cancel</Button>
    </StackPanel>

</Grid>
我有一个数据层,返回绑定的person对象。它包含一个属性列表 绑定到列表框的。为了支持不同类型的属性,这些属性派生自一个公共基类。i、 e.IntegerAttribute:AttributeBase用于表示“年龄”属性

然后,我使用数据模板根据属性的类型呈现正确的控件:

<Window.Resources>
        <DataTemplate DataType="{x:Type reg:IntegerAttribute}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Grid.Column="1" Text="{Binding Name}"/>
                <TextBox Grid.Column="3" Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type reg:TextAttribute}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
                <TextBox Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>           
        </DataTemplate>
        <DataTemplate DataType="{x:Type reg:SingleChoiceAttribute}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
                <ComboBox ItemsSource="{Binding Path=Choices, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
但问题是。我希望变量集属性显示在与固定属性相同的列中。我尝试使用SharedSizeGroup,但似乎不起作用

非常感谢,


NickD

在自定义特性数据模板中,不要使用stackpanel,而是尝试使用具有3列的网格,如包含网格,在内部网格上将IsSharedSizeScope设置为false,在外部网格上将IsSharedGroupSize设置为true,并在两个网格上使用SharedGroupSize

例如:

<Window.Resources>
    <DataTemplate DataType="{x:Type reg:IntegerAttribute}">
        <Grid IsSharedSizeScope="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="groupa" Width="*"/>
                <ColumnDefinition SharedSizeGroup="groupb" Width="10"/>
                <ColumnDefinition SharedSizeGroup="groupc" Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Text="{Binding Name}"/>
            <TextBox Grid.Column="3" Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"/>
        </Grid>
    </DataTemplate>
...
<Grid Name="mainGrid" IsSharedSizeScope="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="groupa" Width="*"/>
        <ColumnDefinition SharedSizeGroup="groupb" Width="10"/>
        <ColumnDefinition SharedSizeGroup="groupc" Width="*"/>
    </Grid.ColumnDefinitions>
...
请注意,如果我没记错的话,共享大小组只适用于自动宽度列,因此上面示例中的一些列定义可能需要更改为自动宽度

另一方面,由于没有列是自动宽度,您也可以根本不使用共享组大小,因为列宽无论如何都会匹配

下面是一个完整的示例,不需要任何代码来演示这一点:

<Window x:Class="gridsh.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <x:ArrayExtension x:Key="data" Type="{x:Type sys:String}">
            <sys:String>Test1</sys:String>
            <sys:String>Test2</sys:String>
            <sys:String>Test3</sys:String>
            <sys:String>Test4</sys:String>
        </x:ArrayExtension>
        <DataTemplate DataType="{x:Type sys:String}">
            <Grid Grid.IsSharedSizeScope="False">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="groupa" Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="xxxxxx"/>
                </Border>
                <TextBlock Text="{Binding}" Grid.Column="1"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid Grid.IsSharedSizeScope="True">
        <Grid Grid.IsSharedSizeScope="False" Height="30" VerticalAlignment="Top" Margin="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="groupa" Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Border BorderBrush="Black" BorderThickness="1">
                <TextBlock Text="WWWWWWWWWWWWWWWWWWWWWWW"/>
            </Border>
            <TextBlock Text="dgdfsg" Grid.Column="1"/>
        </Grid>
        <ListBox ItemsSource="{StaticResource data}" Margin="0,36,0,0" BorderThickness="0" Padding="0"/>
    </Grid>
</Window>

谢谢你的回复。我刚刚尝试了这些更改,不幸的是,虽然它适用于固定宽度的列,但它不适用于Auto或*。如果将“第一列”或“最后一列”设置为“自动”或“*”,对话框将正确打开,列对齐,但最后一列将在对话框中从左向右移动,最终消失。如果想知道为什么会发生这种情况,请假设每次都会重新绘制列…我相信这是一个正确设置IsSharedSizeScope的问题。。。我将再次查看并更新。谢谢,我已按照您的建议将主网格设置为true,将数据模板设置为false。我添加了一个示例来演示此解决方案的工作原理,它不需要任何代码。