在Telerik RadGridView中传递DataContext

时间:2014-12-16 10:22:36

标签: c# wpf telerik radgridview

我使用来自Telerik的c#.NET 4.5和WPF RadControls。

在我的MainWindow我有一个RadTabControl,在我的代码后面我绑定了我的MainViewModel

 this.DataContext = new MainViewmodel();

ItemSource的{​​{1}}绑定在XAML中:

RadTabControl

我还使用 <telerik:RadTabControl ... ItemsSourc={Binding Tabs} .. /> 将不同的内容加载到我的标签中。这些内容为ContentSelector。在一个UserControls上,我使用UserControl与我自己的RadGRidView绑定在后面的代码中:

ItemsSource

TestGridView.ItemsSource = Tasks.GetTasks(); 绑定了它自己的风格:

RadGridView Columns

问题是 <telerik:RadGridView.Columns> <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" Width="*" CellStyle="{StaticResource CellStyle}" /> </telerik:RadGridView.Columns> <Style x:Key="CellStyle" TargetType="{x:Type telerik:GridViewCell}"> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Border BorderBrush="#f2f2f2" BorderThickness="0,0,0,2" Padding="0,5,0,5"> <StackPanel Orientation="Horizontal"> <StackPanel Orientation="Vertical" Margin="10,0,0,0" VerticalAlignment="Top"> <TextBlock Text="{Binding Titel}" /> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Start}" Foreground="#9fa2ae"/> <TextBlock Text=" XXX - XXX " /> <TextBlock Text="{Binding Startzeit}" Foreground="#9fa2ae" /> <telerik:RadButton Height="30" Content="Right Button" Command="{Binding AddTabCommand}" CommandParameter="Tab9999"/> </StackPanel> </StackPanel> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> 没有触发RadButton的{​​{1}}。我在DelegateCommand中的UserControl中也有相同的Button,这很好。

请问有人可以告诉我如何解决MainViewModelRadGridView的{​​{1}}问题?

非常感谢 最好的祝福 RR

PS:我有一个简单的项目,但无法附加

2 个答案:

答案 0 :(得分:0)

发生这种情况的原因是RadButton上的绑定试图在按钮的DataContext上找到AddTabCommand,而不是父窗口。

要解决这个问题,我建议在Window的资源中设置样式,而不是使用它:

Command="{Binding AddTabCommand}"

给窗口命名,并使用:

Command="{Binding ElementName=windowName, Path=DataContext.AddTabCommand}"

答案 1 :(得分:0)

同意Mike所说的在DataContext(ViewModel)中找不到AddTabCommand。 您可以尝试指定命令的完整路径:

Command =“{Binding ElementName = windowName,Path = NameSapce_Name.ViewModelName.AddTabCommand}”。

相关问题