双数组和网格之间的数据绑定

本文关键字:数据绑定 之间 网格 数组 | 更新日期: 2023-09-27 17:50:32

我对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>

在我的resourcedictiondictionary中:

<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>

我想用ButtonSquare对象填充名为MapGrid的UniformGrid,并根据worldMapGrid中对象的类型设置其矩形颜色Fill属性。实际上,MapGridworldMapGrid的视觉表示。

所以我想在这两个对象之间做数据绑定,但我正在努力实现这个概念。有人能告诉我怎么做吗?

双数组和网格之间的数据绑定

下面是一个示例窗口xaml文件:

<Window x:Class="SquareUniformGrid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SquareUniformGrid"
    Title="MainWindow" Height="800" Width="700">
<Window.Resources>
    <local:SquareConverter x:Key="squareConverter"/>
    <DataTemplate x:Key="mountain">
        <Button Style="{StaticResource ButtonSquare}"
                Background="Black"/>
    </DataTemplate>
    <DataTemplate x:Key="desert">
        <Button Style="{StaticResource ButtonSquare}"
                Background="Brown"/>
    </DataTemplate>
    <DataTemplate x:Key="prairie">
        <Button Style="{StaticResource ButtonSquare}"
                Background="Green"/>
    </DataTemplate>
    <local:ButtonTemplateSelector x:Key="selector" Mountain="{StaticResource mountain}" Desert="{StaticResource desert}" Prairie="{StaticResource prairie}"/>
</Window.Resources>
<Grid>
    <ItemsControl Background="Gray" Margin="8" Width="600" Height="600" ItemsSource="{Binding MapGrid}" ItemTemplateSelector="{StaticResource selector}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="4" Columns="4"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

下面是相应的视图模型:

    public class MainWindowViewModel : INotifyPropertyChanged
{
    #region Member Variables
    private List<Square> worldMapGrid = new List<Square>();
    #endregion
    #region Ctr
    public MainWindowViewModel()
    {
        BuildWorldMap();
    }
    #endregion
    #region Public Methods
    #endregion
    #region Private Methods
    private void BuildWorldMap()
    {
        MapGrid.Add(new SquareDesert());
        MapGrid.Add(new SquareDesert());
        MapGrid.Add(new SquareDesert());
        MapGrid.Add(new SquareDesert());
        MapGrid.Add(new SquareMountain());
        MapGrid.Add(new SquareMountain());
        MapGrid.Add(new SquareMountain());
        MapGrid.Add(new SquareMountain());
        MapGrid.Add(new SquarePrairie());
        MapGrid.Add(new SquarePrairie());
        MapGrid.Add(new SquarePrairie());
        MapGrid.Add(new SquarePrairie());
        MapGrid.Add(new SquarePrairie());
        MapGrid.Add(new SquarePrairie());
        MapGrid.Add(new SquarePrairie());
        MapGrid.Add(new SquarePrairie());
    }
    private void RaisePropertyChanged(string _propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(_propertyName));
        }
    }
    #endregion
    #region Properties
    public List<Square> MapGrid
    {
        get
        {
            return this.worldMapGrid;
        }
        set
        {
            this.worldMapGrid = value;
            RaisePropertyChanged("MapGrid");
        }
    }
    #endregion
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
}

下面是DataTemplateSelector的代码:

    public class ButtonTemplateSelector : DataTemplateSelector
{
    #region Member Variables
    #endregion
    #region Ctr
    public ButtonTemplateSelector()
    {
    }
    #endregion
    #region Overrides
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is SquareDesert)
        {
            return Desert;
        }
        if (item is SquareMountain)
        {
            return Mountain;
        }
        if (item is SquarePrairie)
        {
            return Prairie;
        }
        return base.SelectTemplate(item, container);
    }
    #endregion
    #region Public Methods
    #endregion
    #region Private Methods
    #endregion
    #region Properties
    public DataTemplate Mountain
    {
        get;
        set;
    }
    public DataTemplate Desert
    {
        get;
        set;
    }
    public DataTemplate Prairie
    {
        get;
        set;
    }
    #endregion
}

这组代码有效地绘制了一个统一的网格与彩色瓷砖基于worldmapgrid设置。

哎呀,忘了样式了:

<Style x:Key="ButtonSquare" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"/>
</Style>