【问题标题】:Data Binding between a double array and a grid双精度数组和网格之间的数据绑定
【发布时间】:2013-01-03 23:02:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    好的,这是一个示例窗口 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>
    

    【讨论】:

    • 好的..那么也许我不理解你的问题。当我阅读它时,您正在寻找一种将 ButtonSquare 对象绑定到 UniformGrid 的方法,其中 ButtonSquare 的矩形填充绑定到 WorldMapGrid 中 Square 对象的类型。对吗?
    • 是的,实际上我想做 2 个绑定:1) UniformGrid 必须根据双数组 worldMapGrid 填充,worldMapGrid 中的修改会自动修改 UniformGrid 2) UniformGrid中的一个Button对应一个Square,如果是Square is a SquarePrairie`,按钮的颜色必须是绿色,以此类推。
    • 好吧,让我做点什么——过一会儿就会回来
    • 好的,我可以确认您不能绑定到双精度数组。所以你需要想办法将你的双数组转换为单数组,但我现在有一些东西可以工作。更新我之前的答案。
    • 感谢您的帮助,我会根据您给我的想法来解决这个问题。我正在创建一个MyButton : Button,以便我可以添加一个属性来绑定颜色。编辑:我刚刚看到您添加了一些代码,我会尝试一下,稍后再回来验证您的答案是否有效。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2016-03-01
    相关资源
    最近更新 更多