none
WPF应用程序,如何绑定未实现INotifyPropertyChanged的属性? RRS feed

  • 问题

  • 在一个多线程运行的WPF应用程序中,

    我用CountdownEvent 作为线程计数器,需要实时在画面上显示 CountdownEvent.CurrentCount 来反映当前运行线程数的变化情况。

    如果是自己创建的类的话,可以为类实现INotifyPropertyChanged接口来实现画面的自动刷新,但是像CountdownEvent 这样的情况怎样才能做到数据绑定后自动刷新呢?

    //并发线程计数 protected CountdownEvent _ThreadCount = new CountdownEvent(1); //代码里,通过 AddCount 和 Signal进行累加和释放 _ThreadCount.AddCount(); _ThreadCount.Signal();

    //画面上需要用到

    int runningThreadCount = _ThreadCount.CurrentCount;


    2014年4月29日 7:24

答案

  • 可以创建一个类来包装一下,如下所示:

        public class MyCountdownEvent : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private CountdownEvent _count = new CountdownEvent(1);
    
            public void AddCount()
            {
                _count.AddCount();
                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurrentCount"));
                }
            }
    
            public void Signal()
            {
                _count.Signal();
                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurrentCount"));
                }
            }
    
            public int CurrentCount
            {
                get
                {
                    return _count.CurrentCount;
                }
            }
        }

    这样就能使用绑定更新了,以下是示例代码:

    <Window x:Class="WpfApplication4.Window4"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window4" Height="300" Width="300">
        <StackPanel>
            <TextBlock Text="{Binding CurrentCount}"/>
            <Button Content="Add" Click="Button_Click_1"/>
            <Button Content="Signal" Click="Button_Click_2"/>
        </StackPanel>
    </Window>
        public partial class Window4 : Window
        {
            protected MyCountdownEvent _ThreadCount = new MyCountdownEvent();
    
            public Window4()
            {
                InitializeComponent();
                this.DataContext = _ThreadCount;
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                _ThreadCount.AddCount();
            }
    
            private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                _ThreadCount.Signal();
            }
        }


    • 已标记为答案 campsun 2014年4月29日 8:20
    2014年4月29日 7:54

全部回复

  • 可以创建一个类来包装一下,如下所示:

        public class MyCountdownEvent : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private CountdownEvent _count = new CountdownEvent(1);
    
            public void AddCount()
            {
                _count.AddCount();
                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurrentCount"));
                }
            }
    
            public void Signal()
            {
                _count.Signal();
                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurrentCount"));
                }
            }
    
            public int CurrentCount
            {
                get
                {
                    return _count.CurrentCount;
                }
            }
        }

    这样就能使用绑定更新了,以下是示例代码:

    <Window x:Class="WpfApplication4.Window4"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window4" Height="300" Width="300">
        <StackPanel>
            <TextBlock Text="{Binding CurrentCount}"/>
            <Button Content="Add" Click="Button_Click_1"/>
            <Button Content="Signal" Click="Button_Click_2"/>
        </StackPanel>
    </Window>
        public partial class Window4 : Window
        {
            protected MyCountdownEvent _ThreadCount = new MyCountdownEvent();
    
            public Window4()
            {
                InitializeComponent();
                this.DataContext = _ThreadCount;
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                _ThreadCount.AddCount();
            }
    
            private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                _ThreadCount.Signal();
            }
        }


    • 已标记为答案 campsun 2014年4月29日 8:20
    2014年4月29日 7:54
  • 豁然开朗

    果然十分有效

    谢谢~

    2014年4月29日 8:20