我有一个UserControl“UsersGrid”,里面有一个gridview和一些其他对象。
<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的ViewModel:
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");
}
}
}
}
如何从第二个联合国的ViewModel将SelectedItem从“telerik:RadGridView x:Name =”grid_users“”绑定到“SelectedGridUsersRow”。 ?
答案 0 :(得分:0)
您将拥有一个DataContext项,其中包含网格数据的项集合和Selected Item。
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>
答案 1 :(得分:0)
第一个UserControl已经完成了一个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");
}
}
}
}
如果我的两个人都使用,那么建议的解决方案是可行的。有一个DataContent,我不想要......