【问题标题】:WPF/C# DataTemplate changes Layout cause of iconWPF/C# DataTemplate 更改图标的布局原因
【发布时间】:2013-10-19 21:01:27
【问题描述】:

我为 WPF ListBox 写了一个 DataTemplate。但是当我使用 3 个中的 2 个特定图标时,布局会发生变化:

http://i.stack.imgur.com/xMp7F.png

那是我的ItemTemplate:

<ListBox.ItemTemplate>
     <DataTemplate>
          <Grid>
              <Grid.ColumnDefinitions>
                   <ColumnDefinition Width="auto" />
                   <ColumnDefinition Width="auto" />
                   <ColumnDefinition Width="*" />
               </Grid.ColumnDefinitions>
                   <TextBlock Text="{Binding id}" Name="tbID" Margin="5" FontWeight="Bold" FontSize="12" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
                   <Image Height="25" Width="25" Source="{Binding img_src}" HorizontalAlignment="Center" Grid.Column="1" />
                   <StackPanel Margin="10, 0, 0, 0" Grid.Column="2" Orientation="Vertical" >
                       <TextBlock  FontWeight="Bold" FontSize="11" Text="{Binding errorOriginal}"/>
                       <TextBlock  FontSize="10" Text="{Binding errorGerman}" />
                       <!-- Margin="10, 0, 0, 0"   -->
                   </StackPanel>
         </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

这是我的课:

public class compEntry
{
    public int id { get; set; }
    public string img_src { get; set; }
    public string errorOriginal { get; set; }
    public string errorGerman { get; set; }
}

请告诉我我的错误。

//编辑: 当我手动填写我的班级(没有字符串)时,它可以工作:

compList.Add(new compEntry() { id = i, img_src = img, errorOriginal = errText[i], errorGerman = trans }); // not working
compList.Add(new compEntry() { id = i, img_src = img, errorOriginal = "test", errorGerman = "test" });//working

//编辑2: 找到了解决方案。只是修剪字符串:

 compList.Add(new compEntry() { id = i, img_src = img, errorOriginal = errText[i].Trim(), errorGerman = trans.Trim() }); 

【问题讨论】:

  • 所有图片的大小都一样吗?
  • 是的。它们的大小均为 26x26。即使我删除了模板的图像,布局错误仍然存​​在
  • 这似乎有点奇怪......我能想到的唯一可能的解释是错误文本末尾有换行符。
  • 是的。这就是解决方案。我刚刚修剪了它们,现在它可以工作了。谢谢!
  • 请将此问题标记为已回答。我刚刚阅读了整篇文章,发现您已经弄清楚了...

标签: c# .net wpf listbox datatemplate


【解决方案1】:

找到了解决办法。只是修剪字符串:

compList.Add(new compEntry() { id = i, img_src = img, errorOriginal = errText[i].Trim(), errorGerman = trans.Trim() });

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2017-02-07
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多