我对 C#/XAML 比较陌生,并且很难进行数据绑定。我想在 C# 双数组和 XAML 网格之间进行数据绑定。
// This double array is filled with Square objects which are elements of the map
Square[,] worldMapGrid = new Square[4,4];
// For showing an very simple example, we can fill it like this:
worldMapGrid[0,0] = new SquareMountain();
worldMapGrid[0,1] = new SquareDesert();
worldMapGrid[0,1] = new SquarePrairie();
...
我在我的 XAML 代码中有这个:
<UniformGrid x:Name="MapGrid" Width="600" Height="600" Columns="4" Rows="4">
FILL WITH SQUARES HERE
</UniformGrid>
在我的 ResourceDictionnary 中:
<Style x:Key="ButtonSquare" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Viewbox>
<Rectangle x:Name="path1" Fill="CHANGE HERE"/>
</Viewbox>
<ControlTemplate.Triggers>
...
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想用对象填充UniformGrid
命名的 MapGrid ,并根据. 实际上,是 的视觉表示。ButtonSquare
Fill
worldMapGrid
MapGrid
worldMapGrid
所以我想在这两个对象之间进行数据绑定,但我正在为这个概念而苦苦挣扎。有人可以告诉我如何做到这一点吗?