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

时间:2013-01-03 23:02:21

标签: c# wpf xaml

我是C#/ XAML的新手,并且很难进行数据绑定。我想在c#double数组和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对象填充ButtonSquare命名的MapGrid,并根据Fill中对象的类型设置它们的Rectangle颜色worldMapGrid属性。实际上,MapGridworldMapGrid的直观表示。

所以我想在这两个对象之间进行数据绑定,但我正在努力解决这个问题。有人能告诉我怎么做吗?

1 个答案:

答案 0 :(得分: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>

以下是相应的viewmodel:

    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>