List<List<memoryCard>>
我想在我的xmal按钮中显示如何将我的按钮绑定到我想要的数据是我的用户控件:
&LT;
<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
}
我想以这种方式绑定到我的一个网格:
使用此主窗口类:
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 :(得分: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();
}
这与您尝试做的相似吗?
答案 1 :(得分:0)
当您引发PropertyChanged事件时,您的参数名称(您在事件参数中传递的字符串)必须与完全匹配您绑定的属性。这涉及区分大小写。
当属性实际为new PropertyChangedEventArgs("buttonColor")
时,您会ButtonColor
。这将使WPF绑定系统忽略该事件(因为它确定它没有匹配的绑定,因此它不必做任何事情)。您对CardWasFounded