ItemsControl.ItemsSource,绑定不起作用

时间:2018-04-02 19:01:26

标签: c# wpf

这是我与WPF进行数据绑定的第一次尝试。当我运行程序时,虽然设置了DataContext但没有显示任何内容。虚拟鼠标事件处理程序检查ItemsSource,但它为null。

绑定有什么问题?有没有办法调试与字符串相关的绑定内容,或者这总是像在WPF中穿越雷区一样?

备注:不关心错误名称“listBox1”,我从ListBox更改为ItemsControl,因为我觉得更简单,更适合调试。

代码背后:

namespace TestGraphDataBinding
{
    public partial class Window1 : Window
    {
        public ObservableCollection<RectangleModel> Rectangles = new ObservableCollection<RectangleModel>();

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            Rectangles.Add(new RectangleModel(0,0,1.0,0));
        }

        void whatsGoingOn(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Debug.Assert(listBox1.ItemsSource != null);
        }
    }
}

XAML:

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="TestGraphDataBinding.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TestGraphDataBinding"
    Title="TestGraphDataBinding"
    Height="500"
    Width="500">
    <ItemsControl
        ItemsSource="{Binding Rectangles}"
        x:Name="listBox1"
        MouseLeftButtonDown="whatsGoingOn">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas Background="White"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate
                DataType="{x:Type local:RectangleModel}">
                <Rectangle
                    Width="10"
                    Height="10"
                    Fill="Azure">
                    <Rectangle.RenderTransform>
                        <TransformGroup>
                            <TranslateTransform
                                X="{Binding X}"
                                Y="{Binding Y}" />
                            <ScaleTransform
                                ScaleX="{Binding Scale}"
                                ScaleY="{Binding Scale}" />
                            <RotateTransform
                                Angle="{Binding Angle}" />
                        </TransformGroup>
                    </Rectangle.RenderTransform>
                </Rectangle>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

型号:

namespace TestGraphDataBinding
{
    /// <summary>
    /// Description of RectangleModel.
    /// </summary>
    public class RectangleModel
    {
        public double X {get; private set;}
        public double Y {get; private set;}
        public double Scale {get; private set;}
        public double Angle {get; private set;}

        public RectangleModel(double x, double y, double scale, double angle)
        {
            X = x;
            Y = y;
            Scale = scale;
            Angle = angle;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

WPF数据绑定仅适用于公共属性。因此,Rectangles必须是属性,而不是字段:

public ObservableCollection<RectangleModel> Rectangles { get; }
    = new ObservableCollection<RectangleModel>();