【问题标题】:Multiple Datatemplates & Grid.IsSharedSizeScope - Not aligning to content width多个数据模板和 Grid.IsSharedSizeScope - 不与内容宽度对齐
【发布时间】:2013-01-11 17:32:41
【问题描述】:

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

每个模板都有自己的网格布局。模板内每个网格的第一列是一个文本块,我希望它们左对齐。下一项是另一个文本块,它应该与第一个文本块的最大宽度对齐(类似于数据输入表单)。我为此使用了 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 应该可以解决问题。这是正确的还是有其他方法?

提前致谢, -迈克

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    尝试设置

    <ListBox Grid.IsSharedSizeScope="True"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 2020-12-25
      • 1970-01-01
      相关资源
      最近更新 更多