当值被另一个更改时,滑块显示不更新 window
slider display not updated when value is changed by another window
我有一个带有 2 个按钮和一个滑块的 window。
button1 创建一个新 window,button2 更改新 window 的滑块值。但是单击 button2 时滑块显示不会更新。怎么了?
public Window1 newWindow1;
public MainWindow()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
Window1 newWindow1 = new Window1();
newWindow1.Show();
newWindow1.mySlider.Value = 20;
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
newWindow1.mySlider.Value += 10;
}
在 Button1_Click
中,您正在创建 window 的本地实例,而不是使用该字段。
像这样更改您的代码。
private void Button1_Click(object sender, RoutedEventArgs e)
{
newWindow1 = new Window1();
newWindow1.Show();
newWindow1.mySlider.Value = 20;
}
但我强烈建议在使用 WPF 时遵循 MVVM 模式。
我有一个带有 2 个按钮和一个滑块的 window。 button1 创建一个新 window,button2 更改新 window 的滑块值。但是单击 button2 时滑块显示不会更新。怎么了?
public Window1 newWindow1;
public MainWindow()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
Window1 newWindow1 = new Window1();
newWindow1.Show();
newWindow1.mySlider.Value = 20;
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
newWindow1.mySlider.Value += 10;
}
在 Button1_Click
中,您正在创建 window 的本地实例,而不是使用该字段。
像这样更改您的代码。
private void Button1_Click(object sender, RoutedEventArgs e)
{
newWindow1 = new Window1();
newWindow1.Show();
newWindow1.mySlider.Value = 20;
}
但我强烈建议在使用 WPF 时遵循 MVVM 模式。