Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf IViewFor绑定扩展方法如何工作?_Wpf_Mvvm_Data Binding_Reactiveui - Fatal编程技术网

Wpf IViewFor绑定扩展方法如何工作?

Wpf IViewFor绑定扩展方法如何工作?,wpf,mvvm,data-binding,reactiveui,Wpf,Mvvm,Data Binding,Reactiveui,这个问题很简单,但我看不出答案。或者可能不理解Bind方法是如何工作的。 目标是ViewModel和DataContext属性之间的双向绑定 public MainWindow() { InitializeComponent(); this.Bind(this, v => v.DataContext, v => v.ViewModel); } public static readonly DependencyProp

这个问题很简单,但我看不出答案。或者可能不理解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”设置为“WpfApplication1.MainWindow”

但是xaml绑定工作得非常好

<MainWindow 
   DataContext="{Binding RelativeSource={RelativeSource Self}, Path=ViewModel, Mode=TwoWay}" ...
BindTo(this,v=>v.DataContext); this.whenyValue(v=>v.DataContext).BindTo(this,v=>v.ViewModel)

也如预期的那样工作

更新2 问题:这个.Bind(viewModelParam,…)是否忽略viewModelParam参数

示例^

我绑定到_otherViewModel,但在文本框中键入文本时,ViewModel.StrProp发生了更改,而不是_otherViewModel


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

您可以通过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>

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


但是,如果您使用的是ReactiveUI绑定,则根本不需要数据上下文,您应该在任何地方都使用RxUI绑定。请忽略此线程中告诉您如何以错误方式进行操作的其他答案。

这只是一个示例,还是您将主窗口中的datacontext绑定到字符串的原因?是的,只是一个示例,我有一个“普通”的viewmodel类课程。LL:有什么问题吗?请参见此处()代码和我的代码之间只有一个区别是绑定/按链接使用代码:this.whenyValue(x=>x.ViewModel).BindTo(this,x=>x.DataContext);我尝试使用this.Bind(this,v=>v.DataContext,v=>v.ViewModel);为什么他们的代码有效,而我的代码无效?看在上帝的份上,请有人修改一下,并标记为答案!是的,拜托,这家伙写了***FW!>>您根本不需要DataContext,只要在任何难以实现的地方使用RxUI绑定即可。例如,我应该使用单独的IViewf来代替datatemplates(),另一方面,为什么在需要附加属性DataContext的情况下使用.whenyValue(v=>v.DataContext).BindTo(这,v=>v.ViewModel有效?)。
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
}