【发布时间】:2012-08-19 04:43:06
【问题描述】:
是我第一个问题的时间了 :)
我有以下几点:
public class BuilderViewModel : INotifyPropertyChanged
{
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private double _contentScale = 1.0;
public double ContentScale
{
get { return _contentScale; }
set
{
_contentScale = value;
NotifyPropertyChanged("ContentScale");
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#region Commands
bool CanZoomIn() { return true; }
void ZoomInExecute()
{
ContentScale += 1.0;
}
public ICommand ZoomIn { get { return new RelayCommand(ZoomInExecute, CanZoomIn); } }
#endregion
}
以及对应的视图:
<UserControl x:Class="PS_IDE.FormBuilder.View.Builder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PS_IDE.FormBuilder.ViewModel">
<UserControl.DataContext>
<local:BuilderViewModel />
</UserControl.DataContext>
<TextBox Text="{Binding ContentScale}" Width="100" />
</UserControl>
我正在尝试让 BuilderViewModel 中的 ZoomIn 命令更新其视图中的文本框值。该命令是从另一个用户控件 UIBuilder 触发的,其中包括 Builder。如果我从 UIBuilder 调试并触发命令,我可以看到它正确更新 ContentScale。
但是,我的文本框值没有更新(它只显示“1”,即 ContentScale 的初始值)。
我知道我错过了一些东西,希望有人能指出我正确的方向。
编辑:添加了触发命令的控件
<UserControl x:Class="PS_IDE.FormBuilder.UIBuilder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PS_IDE.FormBuilder"
xmlns:ViewModel="clr-namespace:PS_IDE.FormBuilder.ViewModel"
xmlns:View="clr-namespace:PS_IDE.FormBuilder.View" mc:Ignorable="d">
<UserControl.DataContext>
<ViewModel:BuilderViewModel />
</UserControl.DataContext>
<DockPanel LastChildFill="True">
....
<ToolBarTray DockPanel.Dock="Bottom" HorizontalAlignment="Right">
<ToolBar>
<Button Height="24" Width="24" ToolTip="Zoom In" Command="{Binding ZoomIn}">
<Image Source="Images/ZoomIn.png" Height="16"/>
</Button>
....
</ToolBar>
</ToolBarTray>
<View:Builder x:Name="builder" />
</DockPanel>
</UserControl>
【问题讨论】:
-
你能告诉我们触发
ZoomIn命令的控件吗? -
我把它加到原帖的末尾了:)
-
只是为了它,如果你这样做 ContentScale = ContenScale + 1.0; 它是否有效?而不是 ContentScale += 1.0; ?您确定您使用相同的 BuilderViewModel 实例作为两个视图的数据上下文吗?
-
不,同样的问题。我可以在调试器中看到它正在更新,但绑定仍未更新。
-
您在输出窗口中是否收到任何类型的绑定错误消息?
标签: c# .net data-binding mvvm command