2014-12-31 39 views
1

这是我的Employee类及其集合。设置数据模板中列表框的列宽

public class Employee 
{ 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 
    public bool IsHardWorking { get; set; } 
    public string Description { get; set; } 
} 

List<Employee> Employees = new List<Employee>() 
{ 
    new Employee() { IsHardWorking = false, LastName = "Silly", FirstName = "Dude", Description= "this due is a mess" }, 
    new Employee() { IsHardWorking = true, LastName = "Mean", FirstName = "Person", Description= "funny" }, 
    new Employee() { IsHardWorking = false, LastName = "New", FirstName = "Friend", Description= "let her go in next round of layoffs" }, 
    new Employee() { IsHardWorking = true, LastName = "My", FirstName = "Buddy", Description= "simply no comments" }, 
}; 

使用下面显示的数据模板显示数据。

<Grid Loaded="DataLoaded"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="6*" /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <ListBox x:Name="lst1" Grid.Row="0" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="9*" /> 
          <ColumnDefinition /> 
         </Grid.ColumnDefinitions> 


         <StackPanel Orientation="Vertical" Grid.Column="0" HorizontalAlignment="Left"> 
          <StackPanel Orientation="Horizontal"> 
           <Label FontFamily="Tahoma" FontSize="12" VerticalAlignment="Bottom" Content="Last Name" /> 
           <Label FontFamily="Tahoma" FontSize="18" VerticalAlignment="Bottom" Content="{Binding LastName}" /> 
          </StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <Label FontFamily="Tahoma" FontSize="12" VerticalAlignment="Bottom" Content="First Name" /> 
           <Label FontFamily="Tahoma" FontSize="18" VerticalAlignment="Bottom" Content="{Binding FirstName}" /> 
          </StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <Label FontFamily="Tahoma" FontSize="12" VerticalAlignment="Bottom" Content="Details" /> 
           <Label FontFamily="Tahoma" FontSize="18" VerticalAlignment="Bottom" Content="{Binding Description}" /> 
          </StackPanel> 
         </StackPanel> 

         <Image Source="{Binding IsHardWorking, Converter={StaticResource valueToImageConverter}}" Height="50" Grid.Column="1" HorizontalAlignment="Right" /> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5" Grid.Row="1" > 
      <Button x:Name="btnClose" Content="Close" Margin="5" Width="50" /> 
     </StackPanel> 

    </Grid> 

这是它的外观。

enter image description here

我的问题是我想要的图像列应该是右对齐,固定宽度。第一列被分配了大部分的宽度(9 *),但我不知道如何使它看起来像一个列。任何想法

UPDATE
实施@FlatEric建议后,下面就是我得到。 enter image description here
我仍然有很多右侧的空白区域(用黄色矩形标记)。我试图设置Margin为0的图像,但这并没有改变任何东西。

回答

1

只需添加HorizontalContentAlignment='Stretch'到您的列表框元素。除非你在你的valueToImageConverter有什么不寻常的东西可以工作。应该没有必要使用SharedSizeGroup

+0

其实你是对的。使用IsSharedSizeScope添加Horizo​​ntalContentAlignment不能解决问题。一旦我删除了IsSharedSizeScope,它工作。谢谢你的帮助 – BKS

1

为了让所有图像在一列排列,你可以设置在GridColumnDefinitionsSharedSizeGroup财产在DataTemplate

<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="9*" SharedSizeGroup="A" /> 
    <ColumnDefinition Width="*" SharedSizeGroup="B" /> 
</Grid.ColumnDefinitions> 

然后在ListBox

<ListBox x:Name="lst1" Grid.Row="0" Grid.IsSharedSizeScope="True" > 

编辑设置Grid.IsSharedSizeScope="True"

要删除空格右边:

  • 设置ShareSizeGroup只对第二列
  • 添加HorizontalContentAlignment="Stretch"ListBox
+0

谢谢,这当然有帮助但仍不能解决整个问题。我在我的问题中包含了更新的图像和评论。 – BKS