【问题标题】:How works IViewFor Bind extension method?IViewFor 绑定扩展方法如何工作?
【发布时间】:2015-01-30 10:45:04
【问题描述】:

简单的问题,但我没有看到解决方案。或者可能不明白 Bind 方法是如何工作的。 目标是 ViewModel 和 DataContext 属性之间的双向绑定。

    public MainWindow()
    {
        InitializeComponent();

        this.Bind(this, v => v.DataContext, v => v.ViewModel);
    }

    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
        "ViewModel", typeof (string), typeof (MainWindow));

    public string ViewModel
    {
        get { return (string) GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

当我设置 ViewModel 属性时,我得到 InvalidCastException "System.String" to "WpfApplication1.MainWindow"。

但是 xaml 绑定可以完美运行。

<MainWindow 
   DataContext="{Binding RelativeSource={RelativeSource Self}, Path=ViewModel, Mode=TwoWay}" ...

完整的 xaml.cs/xaml 代码在这里 http://pastebin.com/iCKeNS7R

我哪里错了?

更新: 这段代码:

this.WhenAnyValue(v => v.ViewModel).BindTo(this, v => v.DataContext); this.WhenAnyValue(v => v.DataContext).BindTo(this, v => v.ViewModel);

也可以按预期工作

更新 2 问题:this.Bind(viewModelParam, ...) 是否忽略 viewModelParam 参数??

例子^http://pastebin.com/e2aPaGNc

我绑定到 _otherViewModel,但是当在 textBox 中输入文本时,ViewModel.StrProp 发生了变化,而不是 _otherViewModel。

有人知道 this.Bind 是如何工作的吗??

【问题讨论】:

  • 这只是一个例子,还是为什么要将数据上下文从主窗口绑定到字符串?
  • 是的,这只是一个例子,我当然有“普通”视图模型类
  • Will:到底出了什么问题?请参见此处 (github.com/reactiveui/ReactiveUI.Samples/blob/master/…) 代码和我的代码之间只有一个差异是绑定/通过链接使用代码:this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => x.DataContext);我尝试使用 this.Bind(this, v => v.DataContext, v => v.ViewModel);为什么他们的代码有效,而我的无效??

标签: wpf mvvm data-binding reactiveui


【解决方案1】:

Bind 在 ViewModel 和 DataContext 之间不起作用,因为类型不匹配(即我可以将 DataContext 设置为“4”,但现在无法将其分配给 ViewModel)。

但是,如果您使用 ReactiveUI 绑定,则根本不需要 DataContext,您应该在任何地方使用 RxUI 绑定。请忽略此线程上告诉您如何以错误方式做事的其他答案。

【讨论】:

  • 看在上帝的份上,请有人修改这个并标记为答案!
  • 是的,拜托,这个人写了*** FW!
  • >>你根本不需要DataContext,你应该到处使用RxUI绑定很难做到。例如,我应该使用单独的 IViewFor 实现而不是数据模板(stackoverflow.com/questions/25466260/…)另一方面,为什么 this.WhenAnyValue(v => v.DataContext).BindTo(this, v => v.ViewModel 有效???
  • 如果需要附加属性DataContext
【解决方案2】:

您可以通过 XAML 绑定 ViewModel:

<Window x:Class="YourNameSpace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModel="clr-namespace:YourNameSpace"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ViewModel:MainViewModel x:Key="MainViewModel"/>
    </Window.Resources>
    <Grid>
        <TextBox Text="{Binding YourProperty, Mode=TwoWay, Source={StaticResource MainViewModel}, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

您的视图模型应该实现 INotifyPropertyChanged。

public class MainViewModel : INotifyPropertyChanged
{
    private string _yourProperty;
    public string YourProperty
    {
        get { return _yourProperty; }
        set { _yourProperty = value; OnPropertyChanged("YourProperty"); }
    }


    public MainViewModel()
    {
        _yourProperty = "Some string";
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2012-02-06
    • 1970-01-01
    • 2022-07-16
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多