首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个数据模板和Grid.IsSharedSizeScope -不与内容宽度对齐

多个数据模板和Grid.IsSharedSizeScope -不与内容宽度对齐
EN

Stack Overflow用户
提问于 2013-01-12 01:32:41
回答 1查看 997关注 0票数 2

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

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

代码语言:javascript
复制
<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>   

..and我的对象模型

代码语言:javascript
复制
 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来完成这个任务。这是正确的吗?或者还有其他方法吗?

提前谢谢你,-Mike

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-12 01:40:31

尝试设置

代码语言:javascript
复制
<ListBox Grid.IsSharedSizeScope="True"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14283561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档