Silverlight的数据绑定错误 [英] Silverlight DataBinding Error
问题描述
我有一个的GridView
具有 RowDetail
。我想每次用户点击行得到一些细节上的数据库,我用的Telerik 的GridView
。在正常的方式,它是不可能的,或者至少我不知道怎么回事,因为 RowDetail
上下文直接绑定到网格的DataContext
,我要的是比什么GridRow包含它。我发现是,也许我可以设置RowDetailTemplate的DataContext为用户控件
通过命名用户控件
这样我可以参考RowDetail到其他模式。
我的code是这样的
I've a GridView
which has RowDetail
. I want to each time user clicks on the rows get some detail from database, I use Telerik GridView
. In normal way it's not possible or at least I don't know how, because RowDetail
context binded directly to the grid DataContext
, what I want is more than what GridRow contains it. What I found is maybe I can set RowDetailTemplate DataContext to UserControl
by naming the UserControl
so I can reference RowDetail to other model.
My code is something like this
<UserControl
x:Name="mainPageView"
x:Class="Project.Client.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<DataTemplate x:Key="ContactRowDetailTemplate" >
<Grid Background="Transparent"
DataContext="{Binding DataContext.ContactStatModel,
ElementName=mainPageView,Mode=OneTime}">
<Grid.RowDefinitions>
<RowDefinition Height="28" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="Sent SMS Count" Grid.Column="0" Grid.Row="0" />
<TextBlock Text=":" Grid.Column="1" Grid.Row="0" />
<TextBlock Text="{Binding SMSCount}" Grid.Column="2" Grid.Row="0" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<telerik:RadGridView
x:Name="gridView"
AutoGenerateColumns="False" Height="Auto" Grid.Row="3"
ItemsSource="{Binding VOutboxList, Mode=TwoWay}"
SelectedItem="{Binding VOutboxModel, Mode=TwoWay}"
RowDetailsTemplate="{StaticResource ContactRowDetailTemplate}"
LoadingRowDetails="gridView_LoadingRowDetails">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn UniqueName="FirstName" Header="First Name" Width="150" />
<telerik:GridViewDataColumn UniqueName="LastName" Header="Last Name" Width="150" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</UserControl>
但是,这一次我得到这个异常
But this time I get this exception
{Error: System.Exception: BindingExpression_CannotFindElementName}
任何意见将是有益的。 顺祝商祺。
Any advice will be helpful. Best Regards.
推荐答案
这样做的原因是,WPF的和Silverlights DataGrid列生活逻辑树之外,从而使其无法使用绑定源中指定使用的ElementName这是常见的,当引用视图模型属性,如从DataGrid的模板列中的命令。有关此问题的详细信息请参阅:<一href="http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx" rel="nofollow">http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
The reason for this is that WPF's and Silverlights DataGrid columns live outside the logical tree and thus make it impossible to use a binding source specified using ElementName which is common when referencing ViewModel properties such as commands from within DataGrid Template Columns. For more information about this problem see: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
以下行为的柱和它周围的世界之间的胶水类。它是为Silverlight的内置数据网格编写的,但应该很容易适应它的网格的Telerik。它可以是这样的:
The class below act's as glue between the column and the world around it. It was written for Silverlight's built-in DataGrid but should be easy enough to adapt it for the Telerik Grid. It can be used like this:
<DataTemplate x:Key="ContactRowDetailTemplate" >
<Grid Background="Transparent"
DataContext="{Binding ParentDataGrid.DataContext.ContactStatModel,
ElementName=shim,Mode=OneTime}">
<Shims:DataGridShim x:Name="shim"/>
<Grid.RowDefinitions>
<RowDefinition Height="28" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="Sent SMS Count" Grid.Column="0" Grid.Row="0" />
<TextBlock Text=":" Grid.Column="1" Grid.Row="0" />
<TextBlock Text="{Binding SMSCount}" Grid.Column="2" Grid.Row="0" />
</Grid>
</DataTemplate>
public class DataGridShim : FrameworkElement
{
/// <summary>
/// Initializes a new instance of the <see cref="DataGridShim"/> class.
/// prepares the ParentDataGrid property for consumption by sibling elements in the DataTemplate
/// </summary>
public DataGridShim()
{
Loaded += (s, re) =>
{
ParentDataGrid = GetContainingDataGrid(this);
};
}
/// <summary>
/// Gets or sets the parent data grid.
/// </summary>
/// <value>
/// The parent data grid.
/// </value>
public DataGrid ParentDataGrid { get; protected set; }
/// <summary>
/// Walks the Visual Tree until the DataGrid parent is found and returns it
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The containing datagrid</returns>
private static DataGrid GetContainingDataGrid(DependencyObject value)
{
if (value != null)
{
DependencyObject parent = VisualTreeHelper.GetParent(value);
if (parent != null)
{
var grid = parent as DataGrid;
if (grid != null)
return grid;
return GetContainingDataGrid(parent);
}
return null;
}
return null;
}
}
这篇关于Silverlight的数据绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!