2013-01-20 66 views
1

在WP7.5应用程序中,我有一个包含网格的列表框。 这个网格包含两行和两列(2x2)左wpf网格

在网格中,我显示文本框,我的问题是对齐是不好的!我不知道为什么,我设置horizo​​ntalAligement = true,但没有改变!

这里是我的代码:

<ListBox x:Name="ListBoxTiers" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0"> 
<ListBox.ItemTemplate> 
    <DataTemplate> 

     <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Top" > 
      <Grid Margin="20" VerticalAlignment="Top" HorizontalAlignment="Left"> 
       <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 

       <Grid.ColumnDefinitions> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
       <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Grid.Row="0" Grid.Column="0" Margin="0,0,10,0" x:Name="TxtBox_cCodeTiers" Text="{Binding m_strCode}" FontWeight="Bold" FontSize="22" /> 
       <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Grid.Row="0" Grid.Column="1" Margin="0,0,10,0" x:Name="TxtBox_cNomTiers" Text="{Binding m_strNom}" FontWeight="Bold" FontSize="22" /> 
       <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" Margin="0,0,10,0" x:Name="TxtBox_cCPostal" Text="{Binding m_strFonction}" />         
      </Grid> 

     </StackPanel> 

    </DataTemplate> 
</ListBox.ItemTemplate> 

这里是我的结果:

enter image description here

这里是我的类:

public class CTiers 
{ 
    public enum TypeTiers { Client, Fournisseur, Contact, Collaborateur, Commercial, Prospect}; 

    TypeTiers m_TypeTiers { set; get; } 

    public string m_strTypeTiers { get; set; } 
    public string m_strCode { set; get; } 
    public string m_strNom { set; get; } 
    public string m_strPrenom { set; get; } 
    public string m_strTel { set; get; } 
    public string m_strGsm { set; get; } 
    public string m_strFax { set; get; } 
    public string m_strMail { set; get; } 
    public string m_strWebSite { set; get; } 
    public string m_strVille { set; get; } 
    public string m_strCpostal { set; get; } 
    public string m_strRue { set; get; } 
    public string m_strFonction { set; get; } 

    public CTiers() 
    { 

    } 

    public CTiers(TypeTiers oTypeTiers, string strCode, string strNom, string strPrenom, string strTel, string strGsm, string strFax, string strRue,string strVille,string strCPostal,string strMail,string strWebSite,string strFonction) 
    { 
     m_TypeTiers = oTypeTiers; 
     m_strCode = strCode.Trim(); 
     m_strNom = strNom.Trim(); 
     m_strPrenom = strPrenom.Trim(); 
     m_strVille = strVille.Trim(); 
     m_strTel = strTel.Trim(); 
     m_strGsm = strGsm.Trim(); 
     m_strFax = strFax.Trim(); 
     m_strWebSite = strWebSite.Trim(); 
     m_strRue = strRue.Trim(); 
     m_strMail = strMail.Trim(); 
     m_strCpostal = strCPostal.Trim(); 
     m_strTypeTiers = oTypeTiers.ToString().Trim(); 
     m_strFonction = strFonction.Trim(); 
    } 

} 

任何人都可以帮助我吗?

感谢很多:)

问候

+0

StackPanel内部的网格没有意义。删除StackPanel。 – Clemens

+0

在屏幕上向我们展示它现在的样子,以及您想要如何。 – kmatyaszek

+0

你好,我在我的问题中添加了一张图片! :) –

回答

2

你的代码没问题,问题只在于文字修剪。

我在你的问题中使用了相同的xaml并做了一个示例。

案例1:首先,我把所有的字符串修剪

List<Myclass> list = new List<Myclass>(); 
list.Add(new Myclass() { m_strCode = "001", m_strNom = "sample1", m_strFonction = "Fonction1" }); 
list.Add(new Myclass() { m_strCode = "002", m_strNom = "sample2", m_strFonction = "Fonction2" }); 
list.Add(new Myclass() { m_strCode = "003", m_strNom = "sample3 ", m_strFonction = "Fonction3" }); 
list.Add(new Myclass() { m_strCode = "004", m_strNom = "sample4", m_strFonction = "Fonction4" }); 
list.Add(new Myclass() { m_strCode = "005", m_strNom = "sample5", m_strFonction = "Fonction5" }); 
ListBoxTiers.ItemsSource = list; 

,其结果是:(格式化清楚如您所愿)

enter image description here

案例2:在这种情况下,我花了一些带extraa空格的字符串(观察sample1和004)

List<Myclass> list = new List<Myclass>(); 
list.Add(new Myclass() { m_strCode = "001", m_strNom = " sample1", m_strFonction = "Fonction1" }); 
list.Add(new Myclass() { m_strCode = "002", m_strNom = "sample2", m_strFonction = "Fonction2" }); 
list.Add(new Myclass() { m_strCode = "003", m_strNom = "sample3 ", m_strFonction = "Fonction3" }); 
list.Add(new Myclass() { m_strCode = "004 ", m_strNom = "sample4", m_strFonction = "Fonction4" }); 
list.Add(new Myclass() { m_strCode = "005", m_strNom = "sample5", m_strFonction = "Fonction5" }); 
ListBoxTiers.ItemsSource = list; 

而现在的结果是:

enter image description here

因此,问题是你没有正确修剪的文本。注意这一点。

更新:

你走了。你不总是调用构造函数来实例化对象。所以,更好的方法是这样的。

private string _m_strFonction; 
    private string _m_strNom; 
    private string _m_strCode; 

    public string m_strCode 
    { 
     get { return _m_strCode; } 
     set { _m_strCode = value.Trim(); } 
    } 
    public string m_strNom 
    { 
     get { return _m_strNom; } 
     set { _m_strNom = value.Trim(); } 
    } 
    public string m_strFonction 
    { 
     get { return _m_strFonction; } 
     set { _m_strFonction = value.Trim(); } 
    } 
+0

您'对了,我测试过了,这是一个修剪问题,但是我不明白,因为我在我的类属性中使用了.trim().....:\ –

+0

如果你分享你的类代码,我可以建议它出错了 – nkchandra

+0

我在课堂上添加了我的课程:) –

1

我认为这个问题是在MarginGrid文本微调。

Margin集尝试"0, 20, 20, 20"

<ListBox x:Name="ListBoxTiers" ItemsSource="{Binding}" 
     HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0"   
     > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Top" > 
       <Grid Margin="0, 20, 20, 20" VerticalAlignment="Top" HorizontalAlignment="Left"> 
        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="50" /> 
         <ColumnDefinition Width="*" /> 
        </Grid.ColumnDefinitions> 
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Grid.Row="0" Grid.Column="0" Margin="0,0,10,0" x:Name="TxtBox_cCodeTiers" Text="{Binding m_strCode}" FontWeight="Bold" FontSize="22" Loaded="TxtBlock_Loaded" /> 
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Grid.Row="0" Grid.Column="1" Margin="0,0,10,0" x:Name="TxtBox_cNomTiers" Text="{Binding m_strNom}" FontWeight="Bold" FontSize="22" Loaded="TxtBlock_Loaded" /> 
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" Margin="0,0,10,0" x:Name="TxtBox_cCPostal" Text="{Binding m_strFonction}" Loaded="TxtBlock_Loaded" /> 
       </Grid> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

修剪的文字来改善对齐:

<Grid.ColumnDefinitions> 
     <ColumnDefinition Width="50" /> 
     <ColumnDefinition Width="*" /> 
</Grid.ColumnDefinitions> 

private void TxtBlock_Loaded(object sender, RoutedEventArgs e) 
{ 
    TextBlock tb = sender as TextBlock; 
    tb.Text = tb.Text.Trim(); 
} 

如果列包含固定宽度的数据,你可以在XAML指定它

+0

你好,我添加了一个画面在我的问题! :) –

+0

@WalterFabioSimoni我的解决方案不起作用? – kmatyaszek

+0

no :(!没有变化! –

1

你可能更喜欢ListView而不是 列表框。但是,如果您必须使用ListBox,那么您看到的问题是因为每个数据项都有它自己的Grid。换句话说,他们没有共享相同的列。为了规避这一点,宽度添加到每个列定义的:

<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="100" /> 
    <ColumnDefinition Width="*" /> 
</Grid.ColumnDefinitions> 

这将提供一个共同的宽度为所有的左列和右列将在同一地点都开始为好。假设左列中的任何内容都不比宽度设置更宽。

+0

结果是改善了:-)我没有看到里IntelliSense中的stView。 Listview是否可用于WP7.5? –

+0

我不能完全肯定,如果'ListView'是WP7.5框架的一部分。将第一个ColumnDefinion.Width属性调整为适合您的绑定。 –

+0

这并不容易,因为如果我这样做:width =“100”,两个文本块之间有很大的空间:( –