2014-02-10 66 views
0

我正在使用C#/ XAML以及MVVM-Light工具包编程Windows 8.1中的日历。通过绑定调整项目Column

我已经创建了一个ItemsControl作为ItemsPanel的网格,以便我(希望 - 我还没有尝试过这一点)可以将物品放置在该网格内的任何位置。

但是,该网格有3个固定列,我想通过更改该项目的属性来确定将哪个列放入该项目。
我试过这个通过使用绑定,但这种方式不能按预期工作 - 它只适用于静态数字。

下面是创建列表中的代码:

ItemsList = new ObservableCollection<object>(tmpPeriodsList.Select((x, i) => new 
    { 
     ColorHash = x.ColorHash, 
     Index = i, 
     Margin = new Thickness(0,60*i,0, 0), 
     ColumnIndex = ColumnIndex(i), 
    })); 
} 


private int ColumnIndex(int i) 
{ 
    //Purpose: Place the third item the third column 
    if (i == 2) return 2; 
    return 0; 
} 

而且这里的XAML:

<ItemsControl Grid.Column="1" 
       ItemsSource="{Binding Day.ItemsList, Source={StaticResource Locator}}">      
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="100"/> 
        <ColumnDefinition Width="100"/> 
        <ColumnDefinition Width="100"/> 
       </Grid.ColumnDefinitions> 
      </Grid>         
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
       <!-- Those Bindings work fine --> 
       <Grid Height="20" Width="80" Margin="{Binding Margin}"> 
       <Grid.Background> 
        <SolidColorBrush Color="{Binding ColorHash, Converter={StaticResource HexToColorConverter}}"/> 
       </Grid.Background> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 

    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="FrameworkElement"> 
      <!-- This line only works with static numbers (0,1,2) and 
       changes the Column of all Elements --> 
      <Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
</ItemsControl>  

我现在猜测,Grid.Column为整数类型的不行。如列的宽度不是双倍。这可能是真的吗?

我真的没有什么线索可能是错那里...

非常感谢您的帮助!

FunkyPeanut

回答

1

我简化代码有些用于测试目的和结合作品,结合的产品无论是在第0列或第2列;

XAML

<Grid> 
    <ItemsControl Grid.Column="1" 
        ItemsSource="{Binding}"> 
     <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
      </Grid.ColumnDefinitions> 
      </Grid> 
     </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 

     <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <!-- Those Bindings work fine --> 
      <Grid Height="20" 
       Width="80" 
       Margin="{Binding Margin}"> 
      <Grid.Background> 
       <SolidColorBrush Color="Orange" /> 
      </Grid.Background> 
      </Grid> 
     </DataTemplate> 
     </ItemsControl.ItemTemplate> 

     <ItemsControl.ItemContainerStyle> 
     <Style TargetType="FrameworkElement"> 
      <!-- This line only works with static numbers (0,1,2) and 
       changes the Column of all Elements --> 
      <Setter Property="Grid.Column" 
        Value="{Binding ColumnIndex}" /> 
     </Style> 
     </ItemsControl.ItemContainerStyle> 
    </ItemsControl> 
    </Grid> 

代码

public partial class BindGridColumns : Window { 
    private ObservableCollection<object> ItemsList; 

    public BindGridColumns() { 
     InitializeComponent(); 

     ItemsList = new ObservableCollection<object>(); 

     for (int i = 0; i < 7; i++) 
     { 
     ItemsList.Add(new 
     { 
     Index = i, 
     Margin = new Thickness(0, 60 * i, 0, 0), 
     ColumnIndex = ColumnIndex(i), 
     }); 
     } 

     this.DataContext = ItemsList; 
    } 

    private int ColumnIndex(int i) { 
     //Purpose: Place every third item the third column 
     if (i % 3 == 0) return 2; 
     return 0; 
    } 
    } 

结果

enter image description here

由于您没有看到列绑定,请确认您没有收到任何绑定错误。

查看Visual Studio输出窗口中的绑定错误。

+0

嘿,非常感谢您的帮助。我有点不会得到相同的结果,并且输出窗口不显示任何绑定错误。还有什么可能会导致失败?我甚至在代码背后的文件和xaml中复制并粘贴了你的代码 - 唯一不同的是我给ItemsControl一个Name,并且从CodeBehind-File中设置了ItemsControl的DataContext ...我真的不知道问题是什么...再次非常感谢你! – FunkyPeanut

+0

我的代码是否适合您?或者你还没有看到列绑定? –

+0

你的代码不幸也不适合我。真奇怪。我仍然没有看到列绑定。 – FunkyPeanut

相关问题