我正在尝试制作一个进度条,当属性值更改时更新我已经关注了其他问题,但我不知道它有什么问题。
这是 XAML 代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.MainWindow"
Title="MainWindow">
<Grid Margin="0,0,-8,1">
<ProgressBar Value="{Binding Progreso, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" Margin="105,95,207,350"/>
<Button Content="Button" Click="Button_Click" Margin="218,232,333,217"/>
</Grid>
</Window>
它基本上是一个带有绑定的进度条和一个带有监听器的按钮,它将 Progreso 增加 10 这是 C# 代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string sProp)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(sProp));
}
}
float progreso = 10;
public float Progreso
{
get
{
return progreso;
}
set
{
progreso = value;
NotifyPropertyChanged("Progreso");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Progreso = this.Progreso + 10;
}
}
我试图保持简单,但我无法让它工作,对此的任何帮助将不胜感激。
编辑:我也尝试过 UpdateSourceTrigger=PropertyChanged 但这也没有用