2017-06-22 75 views
0

我是WPF的新手,我有一个问题,关注DataContext从MainWindow继承到UserControl, ,它将作为Tabpage附加到MainWindow的Tabcontrol。WPF DataContext从MainWindow继承到UserControl

我的代码片段如下:

UserControlModel.cs

public class UserControlModel : INotifyPropertyChanged 
{ 
    private string _name; 
    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      if (_name != value) 
      { 
       _name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 
    } 

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 

ViewModelLocator.cs

public class ViewModelLocator 
{ 
    private UserControlModel UserControlModel { get; set; } 

    public ObservableCollection<UserControlModel> Users { get; set; } 

    public ViewModelLocator() 
    { 
     Users = new ObservableCollection<UserControlModel> 
     { 
      new UserControlModel { Name = "Albert" }, 
      new UserControlModel { Name = "Brian" } 
     }; 
    } 
} 

MainWindow.xaml

<Window.Resources> 
    <local:ViewModelLocator x:Key="VMLocator" /> 
</Window.Resources> 

<Grid HorizontalAlignment="Left" Height="330" VerticalAlignment="Top" Width="592"> 
    <Grid HorizontalAlignment="Left" Height="45" Margin="0,330,-1,-45" VerticalAlignment="Top" Width="593"> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="490,5,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 
    </Grid> 
    <TabControl HorizontalAlignment="Left" Height="330" VerticalAlignment="Top" Width="592" > 
     <TabItem x:Name="UserControlTabItem" Header="User Control"> 
      <Grid x:Name="UserControlTabpage" Background="#FFE5E5E5"> 
       <local:UserControl VerticalAlignment="Top" DataContext="{Binding Users, Source={StaticResource VMLocator}}" /> 
      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 

我创建一个实例Ø f ViewModelLocator并将用户实例绑定到MainWindow.xaml中的UserControl。

MainWindow.xaml.cs

public MainWindow() 
    { 
     InitializeComponent(); 
    } 

UserControl.xaml

<Grid> 
    <ListBox x:Name="lbUsers" DisplayMemberPath="???" HorizontalAlignment="Left" Height="250" Margin="30,27,0,0" VerticalAlignment="Top" Width="378"/> 
</Grid> 

UserControl.xaml.cs

private ObservableCollection<UserControlModel> _users; 

    public UserControl() 
    { 
     InitializeComponent(); 

     _users = ??? How to reference the Users instance created in MainWindow ??? 

     lbUsers.ItemsSource = _users; 
    } 

其实,我想告诉UserControlModel的Name属性列表框。如果我是正确的,UserControl实例是 ,用Users实例作为MainWindow的DataContext继承。如何在UserControl.xaml.cs的代码隐藏 中引用Users实例?我已检查UserControl构造函数中的DataContext为null!怎么来的?什么是 正确的方式/地方在代码隐藏测试DataContext? 另外,如何在UserControl.xaml中设置ListBox的DisplayMemberPath属性。非常感谢。

+0

您是否解决了以前的问题? https://stackoverflow.com/questions/44670303/wpf-bind-different-usercontrols-to-different-viewmodels。请记住接受答案:https:// meta。stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow – mm8

回答

0

我想你可以设置或用户控制的XAML继承的DataContext这样

UserControl.xaml

<dialogs:Usercontrol DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext}" /> 
+0

添加建议的代码后,我仍然无法获得UserControl.xaml.cs中的用户实例。谢谢。 – user1133555

+0

ViewModelLocator是MainWindow的ViewModel的权利?所以,你需要将其设置为主窗口中的DataContext像下面 MainWindow.xaml.cs 公共主窗口(){ 在InitializeComponent (); this.DataContext = new ViewModelLocator(); } – VipinKV

+0

对不起,UserControl.xaml.cs的DataContext仍然为空。我应该从MainWindow.xaml中删除DataContext属性:?谢谢。 – user1133555

0

您需要将ListBoxItemsSource属性绑定到源集合。由于UserControlDataContextObservableCollection<UserControlModel>,你可以直接绑定到它:

<ListBox x:Name="lbUsers" ItemsSource="{Binding}" DisplayMemberPath="Name" ... /> 

还要确保你没有明确设置UserControl别的地方在你的代码的DataContext

虽然你应该能够引用的DataContext一旦UserControl已加载:

public UserControl() 
{ 
    InitializeComponent(); 
    this.Loaded += (s, e) => 
    { 
     _users = DataContext as ObservableCollection<UserControlModel>; 
    }; 
} 

... ...有没有必要设置ListBoxItemsSource财产代码隐藏。您应该通过在XAML中创建绑定来完成此操作。

+0

UserControl现在可以从MainWindow实例绑定到Users实例。但是,UserControl的DataContext仍然为空。谢谢。 – user1133555