1

我对 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 ,并根据. 实际上,是 的视觉表示。ButtonSquareFillworldMapGridMapGridworldMapGrid

所以我想在这两个对象之间进行数据绑定,但我正在为这个概念而苦苦挣扎。有人可以告诉我如何做到这一点吗?

4

1 回答 1

0

好的,这是一个示例窗口 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>
于 2013-01-03T23:31:42.527 回答