我有一個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」 ?