2017-03-02 87 views
1

我有一個UserControl「UsersGrid」與一個gridview和一些其他對象裏面。綁定屬性的用戶控件的父級用戶控件的屬性wpf mvvm

<UserControl x:Class="MainMenu.Views.UsersGrid" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:ToggleSwitch="clr-namespace:ToggleSwitch;assembly=ToggleSwitch" 
      xmlns:ignore="http://www.galasoft.ch/ignore" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="ignore d" 
      d:DesignHeight="500" d:DesignWidth="250" 
      DataContext="{Binding UsersGrid, Source={StaticResource Locator}}" 
      > 
    <UserControl.Background> 
     <ImageBrush ImageSource="d:/images.jpg"/> 
    </UserControl.Background> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <DockPanel Grid.Row="0" 
        Grid.Column="0" 
        Margin="3 3 3 3" 
        x:Name="panel_useri"> 
      <DockPanel DockPanel.Dock="Top" 
         Margin="2"> 
       <Label DockPanel.Dock="Left" 
          FontFamily="Times New Roman" 
          FontSize="16" 
          FontWeight="Bold" 
          Content="Utilizatori" 
          Margin="0,10,0,0" > 
       </Label> 
       <StackPanel Orientation="Vertical" DockPanel.Dock="Right" > 
        <Label Content="Activi" 
          FontSize="14" 
          HorizontalAlignment="Right" 
          FontWeight="DemiBold" 
          HorizontalContentAlignment="Center" 
          Padding="5,0" 
          Margin="0,0,0,0" /> 
        <ToggleSwitch:HorizontalToggleSwitch 
         IsChecked="True" 
         VerticalAlignment="Top" 
         Margin="0,0,0,2" 
         HorizontalAlignment="Right"/> 
       </StackPanel> 
      </DockPanel> 
      <telerik:RadGridView x:Name="grid_users" 
       DockPanel.Dock="Bottom" 
       AutoGenerateColumns="False" 
       SelectionUnit="FullRow" 
       SelectionMode="Single" 
       FilteringMode="FilterRow" 
       IsReadOnly="True" 
       RowIndicatorVisibility="Collapsed" 
       ShowGroupPanel="False" 
       IsBusy="{Binding IsGridUsersBusy}" 
       ItemsSource="{Binding GridUsersTable}" 
       SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl},AncestorLevel=1},Path=SelectedGridUsersRow, Mode=TwoWay}" 
       > 
       <telerik:RadGridView.Columns> 
        <telerik:GridViewDataColumn Header="Nume" 
               HeaderTextAlignment="Center" 
               Width="1*" 
               TextAlignment="Center" 
               DataMemberBinding="{Binding NumeUser}" /> 
       </telerik:RadGridView.Columns> 
      </telerik:RadGridView> 
     </DockPanel> 
    </Grid> 
</UserControl> 

第二個用戶控件是 「UsersGrid」 的父母和有aloso其他物體...

<UserControl x:Class="MainMenu.Views.Test" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:MainMenu.Views" 
     mc:Ignorable="d" 
     d:DesignHeight="500" d:DesignWidth="700" 
     DataContext="{Binding Test, Source={StaticResource Locator}}" 
     > 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="1*" MaxWidth="200"/> 
     <ColumnDefinition Width="2*"/> 
    </Grid.ColumnDefinitions> 

    <local:UsersGrid x:Name="oUsers" Grid.Column="0" /> 

    <DockPanel Grid.Column="1" > 
     <StackPanel> 
      <TextBox Text="here some value of item selected from gridview" /> 
     </StackPanel> 
    </DockPanel> 


</Grid> 
</UserControl> 

視圖模型第二UC的:

namespace MainMenu.ViewModel { 
public class TestViewModel : ViewModelBase { 

    public TestViewModel() { 
    } 
    private GridUsers_row _selectedGridUsersRow; 
    public GridUsers_row SelectedGridUsersRow 
    { 
     get { return _selectedGridUsersRow; } 
     set 
     { 
      _selectedGridUsersRow = value; 
      RaisePropertyChanged("SelectedGridUsersRow"); 
     } 
    } 
} 
} 

我怎樣才能從綁定的SelectedItem 「telerik:RadGridView x:Name =」grid_users「」從第二個UC的ViewModel到「SelectedGridUsersRow」 ?

回答

0

您將擁有一個DataContext項目,其中包含網格數據的項目集合和選定項目。

public class TestViewModel : ViewModelBase 
{ 
    public TestViewModel() { } 

    private ObservableCollection<User> _users; 
    public ObservableCollection<User> Users 
    { 
     get { return _users; } 
     set 
     { 
      _users= value; 
      RaisePropertyChanged("Users"); 
     } 
    } 

    private User _selectedUser; 
    public User SelectedUser 
    { 
     get { return _selectedUser; } 
     set 
     { 
      _selectedUser= value; 
      RaisePropertyChanged("SelectedUser"); 
     } 
    } 
} 

你的XAML看起來大致是這樣的:

<UserControl x:Class="MainMenu.Views.Test" ... 
      DataContext="{Binding MyTestViewModel, Source={StaticResource Locator}}"> 
    <Grid> 
     ... 

     <local:UsersGrid x:Name="oUsers" Grid.Column="0" /> 

     <DockPanel Grid.Column="1" > 
      <StackPanel> 
       <TextBox Text="{Binding SelectedUser.Name}" /> 
      </StackPanel> 
     </DockPanel> 
    </Grid> 
</UserControl> 

<!-- Note that DataContext is NOT set here. Because it is not set, 
<!--- it will use DataContext from parent item, which is TestViewModel --> 
<UserControl x:Class="MainMenu.Views.UsersGrid" ...> 
    ... 

    <telerik:RadGridView x:Name="grid_users" 
        ItemsSource="{Binding Users}" 
        SelectedItem="{Binding SelectedUser}" 
        ...> 
    ... 
</UserControl> 
0

首先用戶控件具有allredy一個ViewModel:

namespace MainMenu.ViewModel { 
public class UsersGridViewModel : ViewModelBase { 
    public UsersGridViewModel() { 
     Init(); 
    } 

    private void Init() { 
     using (var conn = new FbConnection(Config.connString)) { 

      //populate users grid 
      IEnumerable<GridUsers_row> c_useri = conn.Query(@"select aa.id, aa.nume, aa.dela 
                  from useri aa 
                  where aa.deleted=0 and aa.dela is not null 
                  order by aa.nume") 
               .Select(row => new GridUsers_row() { 
                IdUser = row.ID, 
                NumeUser = row.NUME, 
                ldDela = row.DELA.ToString(), 
               }); 
      Application.Current.Dispatcher.Invoke(() => { 
       GridUsersTable.Clear(); 
       GridUsersTable.AddRange(c_useri); 
      }); 

     } 
    } 

    //c_useri 
    private ObservableRangeCollection<GridUsers_row> _gridUsers = new ObservableRangeCollection<GridUsers_row>(); 
    public ObservableRangeCollection<GridUsers_row> GridUsersTable 
    { 
     get { return _gridUsers; } 
     set 
     { 
      if (_gridUsers == value) 
       return; 
      _gridUsers = value; 
      RaisePropertyChanged("GridUsersTable"); 
     } 
    } 
} 
} 

所提出的解決方案將工作,如果我的兩個U.C.有一個單一的DataContent,我不想...

相關問題