Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式访问UniformGrid ItemTemplate_C#_Wpf_Datatemplate_Itemscontrol - Fatal编程技术网

C# 以编程方式访问UniformGrid ItemTemplate

C# 以编程方式访问UniformGrid ItemTemplate,c#,wpf,datatemplate,itemscontrol,C#,Wpf,Datatemplate,Itemscontrol,我正在尝试使用WPF构建一个宾果游戏模拟器,以了解更多关于WPF的信息,但我很难弄清楚如何以编程方式更改模板 我只使用VS2010中的默认WPF应用程序,因此我有MainWindow.xaml、App.xaml和MainWindow.xaml.cs 我想访问的原因是,如果bingo号码显示为所选,则要更改绑定的模板 我已经在我的代码隐藏文件中进行了尝试,但我认为在这种情况下不起作用 下面是我如何设置主窗口和应用程序xaml文件的 MainWindow.xaml App.xaml 检查这里,

我正在尝试使用WPF构建一个宾果游戏模拟器,以了解更多关于WPF的信息,但我很难弄清楚如何以编程方式更改
模板

我只使用VS2010中的默认WPF应用程序,因此我有MainWindow.xaml、App.xaml和MainWindow.xaml.cs

我想访问
的原因是,如果bingo号码显示为所选,则要更改绑定的模板

我已经在我的代码隐藏文件中进行了尝试,但我认为在这种情况下不起作用

下面是我如何设置主窗口和应用程序xaml文件的

MainWindow.xaml

App.xaml

检查这里,您会发现代码非常接近您需要的内容,但需要做一些工作

我会建议一些重建

例如,将ItemsControl的ItemsSource绑定到CardbNumber。它是一个int列表还是一个CustomClass列表,例如BingoNumClass。如果它是一个自定义类,则添加一个boolean-IsBingo-boolean属性,您将通过撤销将其变为false。无论何时选择宾果游戏号码,它都将从您的代码变为真,并且您将更新CardBNumber列表中的项目

然后,您可以使用IsBingo属性上的Datatriggers扩展ItemTemplate,以在选择后立即更改其外观-从CardBNumber列表中变为true。

选中此项,您将发现代码与您需要的内容非常接近,但需要做一些工作

我会建议一些重建

例如,将ItemsControl的ItemsSource绑定到CardbNumber。它是一个int列表还是一个CustomClass列表,例如BingoNumClass。如果它是一个自定义类,则添加一个boolean-IsBingo-boolean属性,您将通过撤销将其变为false。无论何时选择宾果游戏号码,它都将从您的代码变为真,并且您将更新CardBNumber列表中的项目


然后,您可以在IsBingo属性上使用Datatriggers扩展ItemTemplate,以在选择后立即更改其外观-从您的CardbNumber列表中变为true。

谢谢,我还没有看这个。我会检查一下,然后再给你回复。我想知道你之前在编辑中添加的内容是否可行!我只是不知道具体怎么做。目前CardbNumber是一个列表。所以我在想,就像你说的,只需创建一个新类,它将表示一个宾果空间,具有string number属性和bool checked属性!是的,确实如此。实现INotifyPropertyChanged接口,更新bool值后,您可以使用xaml样式的Datatriggers捕捉此事件。如果你想让我说得更具体些,我可以,我会,但星期一。铜!谢谢,我还没看过。我会检查一下,然后再给你回复。我想知道你之前在编辑中添加的内容是否可行!我只是不知道具体怎么做。目前CardbNumber是一个列表。所以我在想,就像你说的,只需创建一个新类,它将表示一个宾果空间,具有string number属性和bool checked属性!是的,确实如此。实现INotifyPropertyChanged接口,更新bool值后,您可以使用xaml样式的Datatriggers捕捉此事件。如果你想让我说得更具体些,我可以,我会,但星期一。铜!
<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>