2

我的第一个问题的时间:)

我有以下内容:

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>
4

2 回答 2

1

使用两个视图中的设置:

<UserControl.DataContext>
    <local:BuilderViewModel />
</UserControl.DataContext>

您基本上是在创建两个视图模型,每个视图一个。因此,当您的命令更新属性时,它会在其中一个视图模型上执行此操作,但您的文本框绑定到不同的视图模型。

要解决它,DataContextBuilder.xaml

此外,您需要将您的控件传递DataContext给您的Builder控件(这两个视图将共享相同的视图模型)。

所以修改你的 UIBuilder.xaml:

<View:Builder x:Name="builder" DataContext="{Binding}" />
于 2012-08-23T17:07:03.033 回答
0

在绑定中使用 Mode TwoWay

 Text ="{Binding ElementName=BuilderViewModel,
         Path=ContentScale,
         Mode=TwoWay,
         UpdateSourceTrigger=PropertyChanged}"

注意:使用可观察集合来发送通知

于 2012-08-23T15:48:54.780 回答