【发布时间】:2017-10-18 05:08:31
【问题描述】:
我有一个带有滑块和两个文本块的简单 WPF 窗口。随着滑块的移动,它会更新数据绑定对象。现在第一个文本块更新,而第二个没有。为什么?
你可能会说这里没有 INotifyPropertyChanged。但是为什么第一次更新呢?我已经拉够了头发。请帮忙。
我的 WPF 应用程序如下所示。
<Window x:Class="DataTriggerDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataTriggerDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Slider x:Name="MySlider" Margin="5" Minimum="0" Maximum="100"
Value="{Binding TheValue}"/>
<TextBlock Grid.Row="1" Text="{Binding TheValue}" />
<TextBlock Grid.Row="2" Text="{Binding TheValueTwice}" />
</Grid>
</Window>
现在是后面的代码。
using System.Windows;
namespace DataTriggerDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new DataObject();
}
}
public class DataObject
{
private int _theValue;
public int TheValue
{
get { return _theValue; }
set {
_theValue = value;
TheValueTwice = _theValue * 2;
}
}
private int _theValueTwice;
public int TheValueTwice
{
get {
return _theValueTwice;
}
set {
_theValueTwice = value;
}
}
}
}
【问题讨论】:
-
可能是因为 TheValue 是从 WPF 更改的,而 TheValueTwice 是由您的代码更改的。
标签: wpf