c# - 多个数据模板和 Grid.IsSharedSizeScope - 未与内容宽度对齐

标签 c# wpf xaml

我有一个列表框,在其中使用多个数据模板呈现数据,并使用数据模板选择器将数据路由到适当的模板。

每个模板都有自己的使用网格的布局。模板内每个网格的第一列是一个文本 block ,我希望它们向左对齐。下一项是另一个文本 block ,应与第一个文本 block 的最大宽度对齐(类似于数据输入表单)。我正在使用 Grid.IsSharedSizeScope 来实现此目的,但无法实现此目的。下面是我的代码:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">      

<Page.Resources>

 <DataTemplate x:Key="DefaultTemplate">
            <Border BorderThickness="1" CornerRadius="3" BorderBrush="LightGray">
            <TextBlock Text="{Binding Name}"></TextBlock>
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="ShortFieldTemplate">
            <Grid Grid.IsSharedSizeScope="True">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>  
                <TextBlock Text="Age:"  Grid.Column="0"></TextBlock>
                <TextBlock Text="{Binding Age}" Grid.Column="1"></TextBlock>
            </Grid>            
        </DataTemplate>
        <DataTemplate x:Key="LongFieldTemplate">
            <Grid Grid.IsSharedSizeScope="True">

                    <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                        <ColumnDefinition/>                        
                    </Grid.ColumnDefinitions>               
                <TextBlock Text="This should be the name:" Grid.Column="0"></TextBlock>
                <TextBlock Text="{Binding Name}" Grid.Column="1"></TextBlock>                
                </Grid>                      
        </DataTemplate>

        <GridSplitterTextTrim:MyFirstTemplateSelector x:Key="MyFirstTemplateSelector" 
                                                      DefaultDataTemplate="{StaticResource DefaultTemplate}"
                                                      ShortFieldDataTemplate="{StaticResource ShortFieldTemplate}"
                                                      LongFieldDataTemplate="{StaticResource LongFieldTemplate}"
                                                      />

</Page.Resources>

   <Grid x:Name="LayoutRoot" Grid.IsSharedSizeScope="True">

    <Grid Grid.Column="2" Background="Green" >

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <ListBox ItemsSource="{Binding Items}" Grid.Row="0" Grid.Column="0" x:Name="listbox" ItemTemplateSelector="{StaticResource MyFirstTemplateSelector}">  
            </ListBox>
        </Grid>   
   </Grid>  
</Page>   

..和我的对象模型

 public class ShortField : INotifyPropertyChanged
    {
        private string _name;
        public string Age
        {
            get { return _name; }
            set
            {
                _name = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("Age"));
            }
        }

        private string _currentValue;
        public string CurrentValue
        {
            get { return _currentValue; }
            set
            {
                _currentValue = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("CurrentValue"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }

        public static List<ShortField> GetShortFields()
        {
            return new List<ShortField>()
                       {
                           new ShortField() {Age = "10"},
                           new ShortField() {Age = "21"},
                       };
        }
    }

如何正确对齐?我认为 Grid.IsSharedScope 应该可以解决问题。这是正确的还是有其他方法?

提前致谢, -迈克

最佳答案

尝试设置

<ListBox Grid.IsSharedSizeScope="True"

关于c# - 多个数据模板和 Grid.IsSharedSizeScope - 未与内容宽度对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14283561/

相关文章:

c# - 未注册的事件处理程序导致内存泄漏

c# - NHibernate Linq 和 DistinctRootEntity

C#,If 语句中的否定条件是否更快?

c# - 你如何在 C# 中为 Canvas 上的线条设置动画?

.net - 如何在 Silverlight 的组合框中为选定和下拉状态使用不同的模板?

xaml - 如何显示 Callisto WinRT 工具包中的自定义对话框?

c# - ActionExecutedContext 中的 ActionDescriptor.ActionParameters

c# WPF 维护加载程序的单个实例

wpf - 使用 DataTrigger 仅更改左边距或右边距(或两者)

c# - 如何从代码 (.xaml.cs) 打开 Material 设计对话框?