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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
You should either implement the properties as
如果您不这样做,则UI不应该知道何时刷新。
这是如何实现
value
作为依赖关系属性的示例:依赖关系属性默认情况下提供更改通知。您的当前(CLR)属性没有。
You should either implement the properties as dependency properties or implement the
INotifyPropertyChanged
interface and raise thePropertyChanged
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:Dependency properties provide change notifications by default. Your current (CLR) properties do not.