用户控件的数据绑定不响应

时间:2017-05-25 00:18:41

标签: c# wpf xaml

我正试图围绕数据绑定,所以我做了以下玩具示例:
用户控制xaml:

<UserControl x:Class="Ex2.testUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Ex2"
             x:Name="testuc"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="150">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="50"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="{Binding Path=Row, ElementName=testuc}"
               Grid.Column="{Binding Path=Col, ElementName=testuc}"
               Background="Red" Height="50" Width="50"/>
        <Label Grid.Row="0" Grid.Column="1" Background="Blue" Height="50" Width="50"/>
        <Label Grid.Row="0" Grid.Column="2" Background="Blue" Height="50" Width="50"/>
        <Label Grid.Row="1" Grid.Column="0" Background="Blue" Height="50" Width="50"/>
        <Label Grid.Row="1" Grid.Column="1" Background="Blue" Height="50" Width="50"/>
        <Label Grid.Row="1" Grid.Column="2" Background="Blue" Height="50" Width="50"/>
        <Label Grid.Row="2" Grid.Column="0" Background="Blue" Height="50" Width="50"/>
        <Label Grid.Row="2" Grid.Column="1" Background="Blue" Height="50" Width="50"/>
        <Label Grid.Row="2" Grid.Column="2" Background="Blue" Height="50" Width="50"/>
    </Grid>
</UserControl>

用户控制xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace Ex2
{
    public partial class testUC : UserControl
    {
        public testUC() { InitializeComponent(); }

        public int Row
        {
            get { return (int)GetValue(RowProperty); }
            set { SetValue(RowProperty, value); }
        }
        public static readonly DependencyProperty RowProperty =
            DependencyProperty.Register("Row", typeof(int), typeof(testUC));

        public int Col
        {
            get { return (int)GetValue(ColProperty); }
            set { SetValue(ColProperty, value); }
        }
        public static readonly DependencyProperty ColProperty =
            DependencyProperty.Register("Col", typeof(int), typeof(testUC));
    }
}

窗口xaml:

<Window x:Class="Ex2.testWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Ex2"
        xmlns:control="clr-namespace:Ex2"
        mc:Ignorable="d"
        Title="testWindow" Height="300" Width="300">
    <StackPanel>
        <control:testUC x:Name="grid"
                        Row="{Binding Path=Row, Mode=TwoWay}"
                        Col="{Binding Path=Col, Mode=TwoWay}"
                        HorizontalAlignment="Center"/>
        <Button Click="Button_Click" Margin="40,40,40,40">BUTTON</Button>
    </StackPanel>
</Window>

window xaml.cs:

using System.Windows;

namespace Ex2
{
    public partial class testWindow : Window
    {
        private testVMText vm;
        public testWindow()
        {
            InitializeComponent();
            vm = new testVMText();
            DataContext = vm;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            vm.Randomize();
        }
    }
}

查看模型:

using System;
using System.ComponentModel;

namespace Ex2
{
    class testVMText : INotifyPropertyChanged
    {
        public int Row { get; set; }
        public int Col { get; set; }

        public testVMText()
        {

        }
        public void Randomize()
        {
            Random rnd = new Random();
            Row = rnd.Next(0, 3);
            Col = rnd.Next(0, 3);
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

我的意图是每按一次按钮,网格中的一个随机单元格将被涂成红色,但它根本不响应。调试testVMText时,Row和Col的值似乎确实发生了变化,但它并没有传播到testWindow。那是为什么?

0 个答案:

没有答案