我有数据网格,里面有一些列。有行数。我想在用户点击该行的上下文菜单时显示一个窗口。我需要在该行的viewmodel中为某些逻辑设置第一列值。当前我将放置目标作为命令参数ie.gridviewrow传递。以下是我的代码
<telerik:RadGridView.RowStyle>
<Style TargetType="telerik:GridViewRow">
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" ></Setter>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="show Window" Command="{Binding PlacementTarget.Tag.DataContext.ShowChart,RelativeSource={RelativeSource AncestorType=ContextMenu, Mode=FindAncestor}}" CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"></MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</telerik:RadGridView.RowStyle>
如何传递用户点击的特定行的第一列的值?
紫色的MVVM方法吗?如果在这种情况下采用紫外线MVVM方法,那么解决方案是什么?
答案 0 :(得分:2)
从理论上讲,是的,因为访问视图的唯一方法是通过INotifyPropertyChanged和IErrorDataInfo。但是,这取决于你要做什么。如果你想改变UI元素的可见性,我会对MVVM模式产生影响,因为另一种方式对我来说似乎很复杂。我建议告诉你究竟想做什么,也许我能帮助你:)。
答案 1 :(得分:0)
也许您可以尝试为网格的行创建一个新的ViewModel类,每个列都是ViewModel的一个属性。因此,您可以轻松地将行的数据上下文绑定到该ViewModel,并将列绑定到其属性。
答案 2 :(得分:0)
自从你粗略地编辑了这个问题后,我会发布另一个答案。
如果在视图中使用数据网格,则视图(XAML代码)应如下所示:
<Window x:Class="CareerTrackWpfClient.Views.User_Main_Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="User_Main_Window" Background="Black">
<Window>
<Grid HorizontalAlignment="Center" Visibility="Collapsed" Name="gridbooks">
<DataGrid Height="650" HorizontalAlignment="Stretch" Name="BooksGrid" RowHeight="90" ColumnWidth="200"
ColumnHeaderHeight="40" HeadersVisibility="Column" Background="Transparent" RowBackground="DarkGray"
AlternatingRowBackground="LightBlue" BorderBrush="Gray"
BorderThickness="2" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Book #" Width="220" Binding="{Binding BookID}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Blue"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Book Title" Width="220" Binding="{Binding Title}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Blue"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
你应该有这样的模型
public class Book
{
public int BookID {get; set;}
public string Title {get;set;}
}
现在,要么你做一个填充列表的ViewModel(就像Zarzan所说的那样) - 这样你就会尊重MVVM模式。或者在代码beheind中,在构造函数中,抛出InitializeComponent()方法,你写这样的东西
List<Book> booksProvider=new List<Book>();
booksProvider.Add(new Book{BookID=1,"Book 1"}) ;
booksProvider.Add(new Book{BookID=2,"Book 2"}) ;
gridbooks.ItemsSource=booksProvider;
从我的角度来看,使用这个UI组件(DataGrid)是非常好的。它有适当的验证。尝试在BookID字段中输入字符串,它将通知您。它非常灵活,您无需安装任何东西。
希望它能帮助尽可能多的开发人员。