c# WPF。绑定属性更改,但在多线程时不更新UI。

时间:2022-06-29 21:00:04

I have a project, where I bind TextBlock's Text property with a get/set in the codebehind. However, when the application loads, changing binding property can not update the ui.

我有一个项目,我将TextBlock的文本属性与后面的代码中的get/set绑定在一起。但是,当应用程序加载时,更改绑定属性不能更新ui。

MainWindow.xaml:

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox SelectionMode="Multiple" x:Name="lstFileManager" HorizontalContentAlignment="Stretch" Background ="Transparent" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Visibility="{Binding ui_visiable}" Margin="5,0,0,0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="{Binding ui_complete_percentage}"/>
                            <ColumnDefinition Width="{Binding ui_incomplete_percentage}"/>
                        </Grid.ColumnDefinitions>
                        <Rectangle Opacity="0.8" Grid.RowSpan="2" Grid.Column="0" Fill="{Binding ui_complete_color}" />
                        <Rectangle Opacity="0.8" Grid.RowSpan="2" Grid.Column="1" Fill="{Binding ui_incomplete_color}"/>
                        <TextBlock Margin="20,10,20,10" Text="{Binding ui_tip_text}" Grid.ColumnSpan="2" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" MinHeight="20" FontSize="12" FontFamily="Microsoft YaHei"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="Margin" Value="0,0,0,1"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</Window>

MainWindow.xaml.cs:

MainWindow.xaml.cs:

using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace WpfApplication1 {
    /// <summary>
    /// MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {

        public MainWindow() {
            InitializeComponent();
            Bind();
            RefreshFileList();
        }

        #region DataBind
        public class FileListViewItem {
            public string ui_visiable { get; set; }
            public string ui_complete_percentage { get; set; }
            public string ui_incomplete_percentage { get; set; }
            public string ui_complete_color { get; set; }
            public string ui_incomplete_color { get; set; }
            public string ui_tip_text { get; set; }
        }
        ObservableCollection<FileListViewItem> FileListViewItemGroup = new ObservableCollection<FileListViewItem>();
        public void Bind() {
            lstFileManager.ItemsSource = FileListViewItemGroup;
        }
        #endregion

        #region Refresh File List
        private void RefreshFileList() {
            //new Thread((process) => {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => {
                    FileListViewItemGroup.Clear();
                }));
                for (int i = 0; i < 10; i++) {
                    FileListViewItem flvi = new FileListViewItem {
                        ui_tip_text = "1",
                        ui_visiable = "Visiable",
                        ui_complete_percentage = "0*",
                        ui_incomplete_percentage = "100*",
                        ui_complete_color = "White",
                        ui_incomplete_color = "#FFFF43FF"
                    };
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => {
                        FileListViewItemGroup.Add(flvi);
                    }));
                }
                // those code below did not make sence when I am using mutli-threading.
                FileListViewItemGroup[0].ui_complete_percentage = "100*";
                FileListViewItemGroup[0].ui_incomplete_percentage = "0*";
                FileListViewItemGroup[0].ui_tip_text = "what's wrong!";
            //}).Start();
        }
        #endregion

    }
}

The problem is that when I'm trying to use multi-thread, those code (FileListViewItemGroup[0].ui_complete_percentage = "100*";) will not work.

问题是,当我尝试使用多线程时,那些代码[0]。ui_complete_百分数= "100*";)将不起作用。

I've working on it the whole night and I don't know why. Any body can fix the code or have some explain to help me out? Thanks.

我整晚都在做这件事,不知道为什么。谁能帮我修改一下代码或者解释一下?谢谢。

1 个解决方案

#1


0  

(Problem solved in the comments and by question edit from the OP. Converted into a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

(问题在评论和问题编辑从op.00转换成一个社区wiki回答。查看没有答案的问题,但在评论中解决(或在聊天中扩展)

The OP wrote:

OP写道:

Problem solved, here is my code:

问题解决了,这是我的代码:

 #region DataBind
    public class FileListViewItem : INotifyPropertyChanged
    {
        public string ui_visiable { get; set; }
        public string ui_complete_percentage { get; set; }
        public string ui_incomplete_percentage { get; set; }
        public string ui_complete_color { get; set; }
        public string ui_incomplete_color { get; set; }
        private string _ui_tip_text;
        public string ui_tip_text
        {
            get { return _ui_tip_text; }
            set
            {
                _ui_tip_text = value;
                RaisePropertyChanged("ui_tip_text");
            }
        }

        #region Implement INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
    ObservableCollection<FileListViewItem> FileListViewItemGroup = new ObservableCollection<FileListViewItem>();
    public void Bind() {
        lstFileManager.ItemsSource = FileListViewItemGroup;
    }
    #endregion

Thanks PoweredByOrange and Nick for their help :)

感谢PoweredByOrange和Nick的帮助:

#1


0  

(Problem solved in the comments and by question edit from the OP. Converted into a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

(问题在评论和问题编辑从op.00转换成一个社区wiki回答。查看没有答案的问题,但在评论中解决(或在聊天中扩展)

The OP wrote:

OP写道:

Problem solved, here is my code:

问题解决了,这是我的代码:

 #region DataBind
    public class FileListViewItem : INotifyPropertyChanged
    {
        public string ui_visiable { get; set; }
        public string ui_complete_percentage { get; set; }
        public string ui_incomplete_percentage { get; set; }
        public string ui_complete_color { get; set; }
        public string ui_incomplete_color { get; set; }
        private string _ui_tip_text;
        public string ui_tip_text
        {
            get { return _ui_tip_text; }
            set
            {
                _ui_tip_text = value;
                RaisePropertyChanged("ui_tip_text");
            }
        }

        #region Implement INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
    ObservableCollection<FileListViewItem> FileListViewItemGroup = new ObservableCollection<FileListViewItem>();
    public void Bind() {
        lstFileManager.ItemsSource = FileListViewItemGroup;
    }
    #endregion

Thanks PoweredByOrange and Nick for their help :)

感谢PoweredByOrange和Nick的帮助: