数组绑定简单示例不起作用

Array Binding simple example doesn't work

我是 xaml 的新手,我正在尝试了解 Binding 问题。我对 Array Binding 有疑问。我创建了这个非常简单的示例:我有一个包含三个图像的堆栈面板。每张图片都有一个RotationTransform。每个Angle由一个数组元素得到(数组是一个DependencyPropertyRotations)。这是 xaml 简单文件:

<StackPanel Orientation="Vertical">
        <Image Source="/Assets/knife.png" Width="50" Height="100" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <RotateTransform Angle="{Binding ElementName=pageRoot, Path=Rotations[0], Mode=OneWay}"/>
            </Image.RenderTransform>
        </Image>

        <Image Source="/Assets/fork.png" Width="50" Height="100" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
        <Image.RenderTransform>
            <RotateTransform Angle="{Binding ElementName=pageRoot, Path=Rotations[1], Mode=OneWay}"/>
        </Image.RenderTransform>
        </Image>

        <Image Source="/Assets/spoon.png" Width="50" Height="100" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <RotateTransform Angle="{Binding ElementName=pageRoot, Path=Rotations[2], Mode=OneWay}"/>
            </Image.RenderTransform>
        </Image>

        <Button x:Name="actionButton" Content="Try Binding!" 
                Click="Op_Click"/>            
    </StackPanel>

这是我的 c# class:

public sealed partial class MainPage : Page
    {        
        public static readonly DependencyProperty RotationsProperty = DependencyProperty.Register("Rotations", typeof(double[]), typeof(MainPage),
            new PropertyMetadata(new double[3]));

        public double[] Rotations
        {
            get { return (double[])GetValue(RotationsProperty); }
            set { SetValue(RotationsProperty, value); }
        }

        private void Op_Click(object sender, RoutedEventArgs e)
        {
            Rotations[0] = 180;
            Rotations[1] = 130;
            Rotations[2] = 350;
        }

        public MainPage()
        {
            this.InitializeComponent();
            Rotations[0] = 20;
            Rotations[1] = 90;
            Rotations[2] = 180;
        }
    }

绑定仅在第一次(启动时)有效。当我单击按钮(更改 Rotations 数组)时,绑定不起作用,它在我的图像中被完全忽略。

这是一个非常简单的例子,所以很明显我遗漏了一些关于绑定问题的东西。

我想问题是您更改了数组条目的值,但没有更改数组本身,所以 'SetValue' 没有被调用。您可以尝试将 'Rotations' 设置为一个新数组。

this.Rotations = new double[] {180, 130, 350};

编辑:我用我的更改测试了您的代码并且它有效。另一个建议是为数组条目编写一个 setter 方法并调用 'SetValue' 或(如评论中建议的那样)使用 'INotifyPropertyChanged' 而不是 DependencyProperty。

试试下面的代码:

Xaml:

<StackPanel Orientation="Vertical">
    <Image Source="/Assets/knife.png" Width="50" Height="100" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
        <Image.RenderTransform>
            <RotateTransform Angle="{Binding Rotations[0], Mode=OneWay}"/>
        </Image.RenderTransform>
    </Image>

    <Image Source="/Assets/fork.png" Width="50" Height="100" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
    <Image.RenderTransform>
        <RotateTransform Angle="{Binding Rotations[1], Mode=OneWay}"/>
    </Image.RenderTransform>
    </Image>

    <Image Source="/Assets/spoon.png" Width="50" Height="100" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
        <Image.RenderTransform>
            <RotateTransform Angle="{Binding Rotations[2], Mode=OneWay}"/>
        </Image.RenderTransform>
    </Image>

    <Button x:Name="actionButton" Content="Try Binding!" 
            Click="Op_Click"/>            
</StackPanel>

在后面的代码中设置 DataContext 如下:

this.DataContext = 这个;

好的,我找到了使用 ObservableCollection 的解决方案:xaml 文件保持不变,但 c# class 改变为:

public sealed partial class MainPage : Page
    {        
        public static readonly DependencyProperty RotationsProperty = 
            DependencyProperty.Register("Rotations", typeof(ObservableCollection<double>), typeof(MainPage),
            new PropertyMetadata(new ObservableCollection<double>()));       

        public ObservableCollection<double> Rotations
        {
            get { return (ObservableCollection<double>)GetValue(RotationsProperty); }
            set { SetValue(RotationsProperty, value); }
        }

        private void Op_Click(object sender, RoutedEventArgs e)
        {
            Rotations[0] = 180;
            Rotations[1] = 180;
            Rotations[2] = 320;
        }

        public MainPage()
        {
            this.InitializeComponent();
            Rotations.Add(90);
            Rotations.Add(90);
            Rotations.Add(90);
        }
    }