我正在尝试使用WPF构建一个宾果游戏模拟器以了解有关WPF的更多信息,而且我无法弄清楚如何以编程方式更改<ItemsControl>
模板。
我只使用VS 2010中的默认WPF应用程序,所以我有一个MainWindow.xaml,App.xaml和MainWindow.xaml.cs。
我想访问<ItemTemplate>
的原因是,如果宾果号码按照选择出现,则更改绑定模板。
我在我的代码后面尝试了this possible solution,但我不认为这种情况有效。
以下是我设置MainWindow和App xaml文件的方法
<ItemsControl Name="icBColumn" ItemsSource="{Binding CardBNumbers}"
Grid.Column="0" Grid.Row="2"
ItemTemplate="{StaticResource BingoSquare}"
ItemsPanel="{StaticResource BingoColumn}">
<DataTemplate x:Key="BingoSquare">
<Border Background="{DynamicResource UnmarkedSquare}">
<Label Content="{Binding}" />
</Border>
</DataTemplate>
<RadialGradientBrush x:Key="UnmarkedSquare" GradientOrigin="0.5,0.5"
Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="LimeGreen" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
<ItemsPanelTemplate x:Key="BingoColumn">
<UniformGrid Name="NumbersGrid" Columns="1" Rows="5"/>
</ItemsPanelTemplate>
答案 0 :(得分:1)
检查此link,您会发现代码足够接近您需要但需要一些工作。
我会建议一些重建。
例如,您将ItemsControl的ItemsSource绑定到CardBNumbers。它是一个int列表还是CustomClass列表,例如BingoNumClass。如果它是一个自定义类,则添加一个布尔值IsBingo布尔属性,您将通过defualt转为false。只要选择了宾果号码,它就会从您的代码变为真,您将更新CardBNumbers列表的项目。
然后你可以在IsBingo属性上使用Datatriggers扩展你的ItemTemplate,以便在选中后立即显示它的外观 - 从你的CardBNumbers列表变为true。