WPF数据绑定问题

时间:2010-01-14 23:13:23

标签: c# wpf data-binding checkbox inotifypropertychanged

我写了一个非常简短的应用程序,我正在努力实现以下目标:让CheckBox从代码中更改其状态。我已经连接了INotifyPropertyChanged界面并希望看到一些结果,但显然应用程序什么都不做。数据绑定有问题吗?

Window1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace WpfTEST
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        public Window1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
            this.PropertyChanged += new PropertyChangedEventHandler(Window1_PropertyChanged);
        }

        public bool Flag
        {
            get { return m_flag; }
            set
            {
                m_flag = value; 
                OnPropertyChanged("Flag");
            }
        }

        private bool m_flag = false;

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            this.m_cbox.DataContext = this;

            for (int i = 0; i < 1000; i++)
            {
                Flag = (i % 2 == 0);
                Thread.Sleep(200);
            }


        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        void Window1_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }

        #endregion
    }
}

Window1.xaml

<Window x:Class="WpfTEST.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" x:Name="window">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*" />
            <ColumnDefinition Width="0.5*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.5*" />
            <RowDefinition Height="0.5*" />
        </Grid.RowDefinitions>
        <CheckBox x:Name="m_cbox" Content="Let's see what happens" Grid.Row="2" Grid.Column="2" Grid.RowSpan="1" Grid.ColumnSpan="1" IsChecked="{Binding Path=Flag, ElementName=window, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>

2 个答案:

答案 0 :(得分:2)

我看到你的代码唯一真正的问题是Loaded中的sleep循环。这会导致UI线程在循环持续时间内无响应,因为您将UI线程置于阻塞状态。

有一种更好的方法来测试你是否可以从代码中更改标志并使绑定点火。向窗口添加一个按钮,将单击处理程序连接到该按钮,然后在该单击处理程序中切换Flag - 我对原始代码进行了修改(并删除了睡眠循环),单击按钮以你想要的方式切换复选框的状态。

<Window x:Class="WpfTEST.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" x:Name="window">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*" />
            <ColumnDefinition Width="0.5*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.5*" />
            <RowDefinition Height="0.5*" />
        </Grid.RowDefinitions>
        <!-- I added this button: -->
        <Button Click="Button_Click" Grid.Row="0" Grid.Column="0"/>
        <CheckBox x:Name="m_cbox" Content="Let's see what happens" Grid.Row="2" Grid.Column="2" Grid.RowSpan="1" Grid.ColumnSpan="1" IsChecked="{Binding Path=Flag, ElementName=window, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>

并在代码背后:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace WpfTEST {
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged {
        public Window1() {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
            this.PropertyChanged += new PropertyChangedEventHandler(Window1_PropertyChanged);
        }

        public bool Flag {
            get { return m_flag; }
            set {
                m_flag = value;
                OnPropertyChanged("Flag");
            }
        }

        private bool m_flag = false;

        void Window1_Loaded( object sender, RoutedEventArgs e ) {
            this.m_cbox.DataContext = this;
            Flag = false;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged( string name ) {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        void Window1_PropertyChanged( object sender, PropertyChangedEventArgs e ) {

        }

        #endregion

        private void Button_Click( object sender, RoutedEventArgs e ) {
            Flag = !Flag;
        }
    }
}

除了上面添加按钮及其单击处理程序的修改以及删除该睡眠循环之外,我没有对原始代码进行任何修改(尽管您可以对其进行简化)。

答案 1 :(得分:1)

仅当您的Flag属性是依赖属性时才会起作用。为了让这个以最简单的方式工作,我可以想到我会做以下事情: 将您的绑定更改为:

IsChecked =“{Binding Path = Flag,Mode = TwoWay}”

并在您的代码后面的构造函数中执行此操作:

DataContext = this;

或在XAML中

DataContext =“{Binding RelativeSource = {RelativeSource self}}”

注意:如果要将datacontext设置为窗口,则Flag不需要是依赖项属性。