2014-03-02 83 views
-1

我有視圖模型如何將對象傳遞給ViewModel在MVVM中構建時?

public class VM: DependencyObject, INotifyPropertyChanged 
{ 
    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof(string), typeof(VM), new PropertyMetadata("")); 

    public int Length 
    { 
     get 
     { 
      return Text != null ? Text.Length : 0; 
     } 
    } 

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

,並查看它

<UserControl.DataContext> 
    <local:VM Text="{Binding ControlText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/> 
</UserControl.DataContext> 

<StackPanel> 
    <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"/> 
    <TextBlock Text="{Binding Length}"/> 
</StackPanel> 

是的,觀點也落後

public string ControlText 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("ControlText", typeof(string), typeof(Writer), new PropertyMetadata("")); 

代碼依賴屬性我上輸出了錯誤日誌

System.Windows.Data錯誤:4:無法找到bindin的源代碼g引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.UserControl',AncestorLevel ='1''。 BindingExpression:路徑= ControlText;的DataItem = NULL;目標元素是'VM'(HashCode = 21019086);目標屬性是'文本'(類型'字符串')

當這個技術工作時,我將有工具來構建我的viewmodel。 也許任何人都知道如何將此視圖屬性綁定到viewmodel屬性或知道技術如何正確執行:)

回答

0

如果您在XAML中聲明DataContext,它將在加載XAML時進行初始化。此外,​​不在UserControl中,因此RelativeSource無法在此處工作。

但你可以做到這一點在後面的代碼從用戶控件的構造函數設置的DataContext並做結合自身有:

public MainWindow() 
{ 
    InitializeComponent(); 
    VM viewModel = new VM(); 
    DataContext = viewModel; 
    Binding binding = new Binding("ControlText") { Source = this }; 
    BindingOperations.SetBinding(viewModel, VM.TextProperty, binding); 
} 
1

你似乎是有幾件事情有些困惑。如果您想設置您的視圖模型作爲XAML的UserControl.DataContext,這是確定的,但你從那裏沒有數據綁定到控件:

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

接下來,如果視圖模型設置爲UserControl.DataContext,然後現在,如果你想從XAML訪問ControlText屬性,那麼是當你需要使用RelativeSource Binding

<StackPanel> 
    <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"/> 
    <TextBlock Text="{Binding Length}"/> 
</StackPanel> 

:你可以在UserControl這樣從XAML訪問其屬性。但是,請注意您應該如何參考UserControl的名稱/類型,而不是使用UserControl,因爲如您的錯誤所述,在UserControl中沒有定義ControlText屬性。試試這個:

<StackPanel> 
    <TextBox Text="{Binding ControlText, RelativeSource={RelativeSource 
     AncestorType={x:Type local:YourUserControl}} /> 
    <TextBlock Text="{Binding Length}"/> 
</StackPanel> 

最後,如果你想將數據綁定到從控制控件的ControlText財產,那麼你可以做的是這樣的:

<local:YourUserControl ControlText="{Binding SomeStringProperty}" /> 

更新>>>

當使用MVVM時,我們一般不要嘗試傳遞數據一個數據源通過UIElement傳送到另一個數據源。相反,我們寧願將數據從一個數據項傳遞到另一個數據項。你的問題是你在視圖中實例化你的視圖模型...而是在父視圖模型中實例化它,並傳入你想要的任何值,然後將其設置爲屬性值外部的父控件觀點:

<local:YourUserControl DataContext="{Binding YourChildViewModelPropertyInParentVM}" /> 

一旦你的視圖模型正確的價值觀,那麼你應該不需要ControlText財產。

+0

我知道, 和你顯示我最後feaute是我在作家(UserCntrol)中使依賴屬性的主要原因。 但我想做點這樣的事: 我有字符串屬性 string s {get;設置;} 我創建Writer(UserControl),並通過綁定創建的用戶控件傳遞此字符串。 現在我想要一個UserCOntrol將屬性s傳遞給Viewmodel。 所以我想在Writer.xaml中做 這就是構建Viewmodel通過視圖的要點。 property is s draging throught view to viewmodel – Jan3Sobieski

相關問題