2010-10-04 113 views
0

嘿 我有问题,我有WPF中controlitem绑定到集合

List<List<memoryCard>> 

,我想在我的Xmal位来显示一个按钮,我怎么能我的按键绑定到我想要的数据茨艾伦是我的用户控件:

<

<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}"> 
    <Grid> 
    <!--i think this is the place where i make mistake :--> 
      <TextBlock Text="{Binding Path=CardWasfounded}"/> 
      <Rectangle Margin="4,5,8,2" Stroke="Black" RadiusX="45" RadiusY="45" StrokeThickness="3"/> 
    </Grid> 
    </ControlTemplate> 

    <DataTemplate x:Key="DataTemplate_Level2"> 
     <Button Content="{Binding}" Height="40" Width="50" Margin="4,4,4,4" Template="{DynamicResource ButtonControlTemplate1}"/> 
    </DataTemplate> 

<DataTemplate x:Key="DataTemplate_Level1"> 
    <ItemsControl ItemsSource="{Binding }" ItemTemplate="{DynamicResource DataTemplate_Level2}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</DataTemplate> 

我希望每个按钮都结合到该存储卡

class memoryCard : INotifyPropertyChanged 
{ 
    #region c'tor 
    public memoryCard(Brush _buttonColor) 
    { 
     buttonColor=_buttonColor; 
    } 
    #endregion 

    #region allReadyFoundedCard 

    bool cardWasfounded = false; 
     public bool CardWasfounded 
     { 
      get 
      { 
       return cardWasfounded; 
      } 
      set 
      { 
       if (cardWasfounded != value) 
       { 
        cardWasfounded = value; 
        if (PropertyChanged != null) 
        { 
         PropertyChanged(this, 
         new PropertyChangedEventArgs("cardWasfounded")); 

        } 
       } 
      } 
     } 
     #endregion 

    #region colorofbutton 
     string name = "sdasdas"; 
     public Brush buttonColor; 
     public Brush ButtonColor 
     { 
      get 
      { 
       return buttonColor; 
      } 
      set 
      { 
       if (buttonColor != value) 
       { 
        buttonColor = value; 
        if (PropertyChanged != null) PropertyChanged(this, 
         new PropertyChangedEventArgs("buttonColor")); 
       } 
      } 
     } 
     #endregion 

    #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     #endregion 
} 

人,我想这种方式绑定到我的网格中的一种:使用这个MainWindow类

public MainWindow() 
    { 
     List<List<memoryCard>> lsts = new List<List<memoryCard>>(); 

     for (int i = 0; i < 5; i++) 
     { 
      lsts.Add(new List<memoryCard>()); 

      for (int j = 0; j < 5; j++) 
      { 
       lsts[i].Add(new memoryCard(Brushes.Green)); 
      } 
     } 

     InitializeComponent(); 

     lst.ItemsSource = lsts; 
    } 
+0

在这两个你的属性同样的问题,你在每个相应的制定者复制在NotifyPropertyChanged(字符串信息)的代码。 – Val 2010-10-04 09:23:28

+0

那是什么意思?这不是正确的方式来暗示它吗? – 2010-10-04 09:36:53

+0

方法是正确的,它只是代码的重复。不是检查属性是否改变在每个二传手空,你已经建立了一个方法NotifyPropertyChanged来为你做它。所以只需调用方法。减少代码重复和膨胀。 – Val 2010-10-04 09:42:59

回答

1

好吧,所以从我收集的信息中可以得到一组自定义数据类型的集合,其中包含您想要绑定的颜色。

因此,继承人一个小的演示,你应该(希望)能够扩展。

XAML:

<ItemsControl ItemsSource="{Binding Path=MyCollection}" 
       Height="300" Width="600"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Expander Header="Open Me"> 
       <ItemsControl DataContext="{Binding}" ItemsSource="{Binding}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Button DataContext="{Binding}" 
            Background="{Binding Path=ButtonColor}" 
            Content="{Binding Path=CardWasFounded}"/> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
      </Expander> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

而且在后面的代码:

public ObservableCollection<List<memoryCard>> MyCollection {get; set;} 

public MainWindow() 
{ 
    DataContext = this; 
    MyCollection = new ObservableCollection<List<memoryCard>>(); 

    for (int i = 0; i < 5; i++) 
    { 
     List<memoryCard> list = new List<memoryCard>(); 

     for (int j = 0; j < 5; j++) 
     { 
      list.Add(new memoryCard(Brushes.Green)); 
     } 
     MyCollection.Add(list); 
    } 

    InitializeComponent(); 
} 

这是类似于你想做什么?

+0

谢谢它seemes像正确的路径穿行,但它不为我工作,你可以准确地编辑它,因为它是在你的项目? – 2010-10-04 10:57:48

+0

我已经更新了XAML顶级集合绑定中的问题。我留在我的测试集合中而不是绑定到MyCollection。试试这个新解决方案。如果仍然存在问题,就会将它分解成更细的块。 – Val 2010-10-04 11:09:11

+0

非常感谢你! – 2010-10-04 11:14:32

0

当您引发PropertyChanged事件时,您的参数名称(您在事件参数中传递的字符串)必须与您绑定的属性完全匹配。这涉及区分大小写。

你做new PropertyChangedEventArgs("buttonColor")当地产实际上是ButtonColor。这将使WPF绑定系统忽略该事件(因为它确定它没有匹配的绑定,所以它不必做任何事情)。你有CardWasFounded