WPF进度栏不做更新

发布于 2025-01-25 23:51:24 字数 1783 浏览 2 评论 0原文

嗨,我正在尝试设置一个带有一些文本和一个按钮取消的进度栏,因此我使用了用户控件。我创建了此用户控件(最小值,最大值)的属性,以将其传递到内部进度栏。当我使用这些值初始化用户控制时,它可以工作,但是当我更新值时,进度栏不会移动。 有人知道为什么吗? 当然,进度栏代表的工作是在单独的线程上完成的,并且值确实更新了,并从0到100。xaml

呼叫

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <ProgressBar x:Name="progress_bar_internal" Minimum="{Binding Min}" Maximum="{Binding Max}" Value="{Binding Value}" Foreground="DarkGreen" Height="{Binding Height}" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding ElementName=progress_bar_internal, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </StackPanel>
        <wpfui:Button Icon="PaneClose24" Grid.Column="1" VerticalAlignment="Center" Height="{Binding Height}" x:Name="cancel_btn" Background="DarkRed"/>
    </Grid>

的代码

public partial class ProgressBar_w_Text : UserControl
    {
        
        public ProgressBar_w_Text()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public event RoutedEventHandler Click
        {
            add { cancel_btn.AddHandler(ButtonBase.ClickEvent, value); }
            remove { cancel_btn.RemoveHandler(ButtonBase.ClickEvent, value); }
        }

        public int Value { get; set; }
        public int Max { get; set; }
        public int Min { get; set; }
    }

背后

<uc:ProgressBar_w_Text Visibility="Collapsed" x:Name="progress_bar" Height="50" Click="cancel_patcher" Min="0" Max="100" Value="15"/>

Hi I am trying to set up a progress bar with some text and a button to cancel, so I used a user control. I created properties to this user control (Min,Max,Value) to pass them to the internal progress bar. It works when I initialize my user control with these values but when I update the value the progress bar doesn't move.
Does anyone have any idea why ?
Of course, the work represented by the progress bar is done on a seperate thread and the values are indeed updated and goes from 0 to 100.

XAML

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <ProgressBar x:Name="progress_bar_internal" Minimum="{Binding Min}" Maximum="{Binding Max}" Value="{Binding Value}" Foreground="DarkGreen" Height="{Binding Height}" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding ElementName=progress_bar_internal, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </StackPanel>
        <wpfui:Button Icon="PaneClose24" Grid.Column="1" VerticalAlignment="Center" Height="{Binding Height}" x:Name="cancel_btn" Background="DarkRed"/>
    </Grid>

The code behind

public partial class ProgressBar_w_Text : UserControl
    {
        
        public ProgressBar_w_Text()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public event RoutedEventHandler Click
        {
            add { cancel_btn.AddHandler(ButtonBase.ClickEvent, value); }
            remove { cancel_btn.RemoveHandler(ButtonBase.ClickEvent, value); }
        }

        public int Value { get; set; }
        public int Max { get; set; }
        public int Min { get; set; }
    }

Call

<uc:ProgressBar_w_Text Visibility="Collapsed" x:Name="progress_bar" Height="50" Click="cancel_patcher" Min="0" Max="100" Value="15"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2025-02-01 23:51:24

You should either implement the properties as

如果您不这样做,则UI不应该知道何时刷新。

这是如何实现value作为依赖关系属性的示例:

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
    nameof(Value), typeof(int), typeof(ProgressBar_w_Text));

public int Value
{
    get => (int)GetValue(ValueProperty);
    set => SetValue(ValueProperty, value);
}

依赖关系属性默认情况下提供更改通知。您的当前(CLR)属性没有。

You should either implement the properties as dependency properties or implement the INotifyPropertyChanged interface and raise the PropertyChanged event whenever any property is set to a new value.

If you don't do any of this, the UI cannot be supposed to know when to refresh.

Here is an example of how to implement Value as a dependency property:

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
    nameof(Value), typeof(int), typeof(ProgressBar_w_Text));

public int Value
{
    get => (int)GetValue(ValueProperty);
    set => SetValue(ValueProperty, value);
}

Dependency properties provide change notifications by default. Your current (CLR) properties do not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文