2014-12-06 110 views
0

一個新手綁定問題。我有多個用戶控件在我mainview.xaml叫,如:如何將用戶控件的屬性綁定到MainViewModel並將其datacontext綁定到它自己的viewmodel?

<i:InkRichTextView RichText ="{Binding LastNote}" ... /> 

其中富文本是在InkRichTextView.xaml用戶控件的代碼隱藏依賴屬性和LastNote是mainviewmodel的屬性。

的MainViewModel通過在App.xaml中一個DataTemplate的方式與含蓄的MainView相關,如:

<DataTemplate DataType="{x:Type vm:MainViewModel}"> 
     <v:MainView/> 
</DataTemplate> 

我想設置的用戶控件InkRichTextView自身的視圖模型,它的屬性是在MainViewModel舉行,是這樣的:

<i:InkRichTextView RichText ="{Binding LastNote}" DataContext="{Binding ucFirstControl}" ... /> 

但是當我這樣做,我的用戶控制InkRichTextView失去了LastNote上下文(或失去在它的依賴屬性訪問代碼隱藏,或兩者?) 。

如何維護對LastNote的MainViewModel屬性的綁定,並仍爲用戶控件提供單獨的DataContext? (請記住,UserControl在其代碼隱藏中定義了依賴項屬性)。

感謝您的任何幫助。

+0

啊哈,一個星期,另一人試圖創建一個視圖模型爲他們的用戶控件。想知道爲什麼你會遇到困難嗎?這是因爲*你不應該這樣做*。想想看 - TextBox有一個ViewModel嗎?不會。它暴露了你綁定* * ViewModel的屬性。這就是你的UserControl應該做的。在這裏看到更多的細節:http://stackoverflow.com/search?tab=newest&q=user%3a1228%20is%3aanswer%20usercontrol – Will 2014-12-08 15:34:45

回答

1

通過x:Name屬性爲MainView指定一個名稱。然後,對於RichText屬性,您可以使用如下綁定:RichText = {Binding ElementName = mainViewName,Path = DataContext.LastNote}

真的,您可以使用任何在ElementName屬性中具有MainViewModel作爲其DataContext的控件的綁定。這可能更容易,因爲通過名稱生成DataTemplate的控件可能會非常棘手。注意如何綁定到ChildName的TextBox基於其DataContext隱式地執行,而TextBox旁邊的TextBox通過parentControl(使用ElementName)綁定到DataContext的Name屬性。

XAML:

<Window x:Class="WpfApplication3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="250"> 
    <StackPanel x:Name="parentControl" Orientation="Vertical" DataContext="{Binding}"> 
     <TextBox Text="{Binding Name}"/> 
     <StackPanel DataContext="{Binding ChildViewModel}" Margin="10"> 
      <TextBox Text="{Binding ChildName}"/> 
      <TextBox Text="{Binding ElementName=parentControl, Path=DataContext.Name}"/> 
     </StackPanel> 
    </StackPanel> 
</Window> 

CS:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new MainViewModel(); 
    } 

} 

public class PropertyNotifier : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propname) 
    { 
     var propChanged = PropertyChanged; 
     if (propChanged != null) 
      propChanged(this, new PropertyChangedEventArgs(propname)); 
    } 
} 

public class MainViewModel : PropertyNotifier 
{ 
    private string _name = "MainViewModelProperty"; 
    private ChildViewModel _childViewModel = new ChildViewModel(); 

    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 

    public ChildViewModel ChildViewModel 
    { 
     get { return _childViewModel; } 
     set 
     { 
      _childViewModel = value; 
      OnPropertyChanged("ChildViewModel"); 
     } 
    } 
} 

public class ChildViewModel : PropertyNotifier 
{ 
    private string _childName = "ChildViewModelProperty"; 

    public string ChildName 
    { 
     get { return _childName; } 
     set 
     { 
      _childName = value; 
      OnPropertyChanged("ChildName"); 
     } 
    } 
} 
+0

沒有工作。顯示屏是空白的。請注意,如果我關閉「DataContext = ...」,那麼效果很好,但我無法爲usercontrol使用不同的viewmodel。我堅持繼承? – 2014-12-06 11:56:44

+0

這種方法應該是可行的 - 但細節取決於對象之間的確切關係。你能發佈一個更完整的樣本來重現問題嗎?我希望能給出一個更完整的答案, – Andrew 2014-12-06 21:45:02

+0

請參閱我的錯誤 – Andrew 2014-12-06 21:57:13

相關問題