2013-01-11 83 views
2

我有一个列表框,我使用多个数据模板呈现数据,并使用数据模板选择器将数据路由到适当的模板。多数据模板和Grid.IsSharedSizeScope - 未对齐到内容宽度

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

由于提前, -Mike

回答

3

尝试设置

<ListBox Grid.IsSharedSizeScope="True"