将Textblock的Text属性绑定到Silverlight中具有不同DataContext的组合框selectedItem
我有一个绑定到MyStaff类型对象的数据网格。除了其他属性外,MyStaff还包含一个名为LookupID的列。现在,在我的ViewModel中,我有一个名为Lookups的集合,其中包含每个LookupID的描述 我有一个模板列,在单元格模板中有一个Textblock,在CellEdit模板中有一个Combobox。如何绑定Textblock,使其基于LookupID显示ComboBox中的描述 我知道,如果Textblock和ComboBox的datacontext都很简单,那将非常简单,但事实并非如此 我试过这个,但不起作用。有什么建议吗?如果您能提供有关如何在Silverlight中为不同控件最佳使用不同数据上下文的信息,我们也将不胜感激。为此,我添加了一个指向ViewModel类的静态资源将Textblock的Text属性绑定到Silverlight中具有不同DataContext的组合框selectedItem,silverlight,xaml,silverlight-4.0,Silverlight,Xaml,Silverlight 4.0,我有一个绑定到MyStaff类型对象的数据网格。除了其他属性外,MyStaff还包含一个名为LookupID的列。现在,在我的ViewModel中,我有一个名为Lookups的集合,其中包含每个LookupID的描述 我有一个模板列,在单元格模板中有一个Textblock,在CellEdit模板中有一个Combobox。如何绑定Textblock,使其基于LookupID显示ComboBox中的描述 我知道,如果Textblock和ComboBox的datacontext都很简单,那将非常简单,
<sdk:DataGridTemplateColumn Header="Action Point"
Width="500"
CanUserReorder="False"
HeaderStyle="{StaticResource dthFull2}"
IsReadOnly="False">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding ElementName=LookupList,
Path=SelectedItem.Description}"
MinHeight="24"
VerticalAlignment="Top"
Padding="2"
TextTrimming="WordEllipsis"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<my:AutoCompleteComboBox x:Name="LookupList"
FilterMode="Custom" Margin="2,0,0,0"
SelectedValue="{Binding LookupID, Mode=TwoWay}"
SelectedValuePath="LookupID"
ItemsSource="{Binding Path=AnalysisLookupList.Values,
Source={StaticResource ViewModel}}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
通常,行的ViewModel中有一个公共属性,两个控件(来自CellTemplate和CellEditingTemplate)都可以绑定到该属性,因此是同步的。不要混淆ComboBox的ItemsSource和DataContext,它们可以是完全不同的对象而不会引起任何问题?是否有理由不在ViewModel上使用公共属性?数据网格绑定到名为MyStaff的集合,所选行绑定到名为Selected Staff的属性。除其他外,选定的Staff包含一个名为LookupID的列。在我的ViewModel中,我有一个名为LookupList的集合。现在我试图实现的是网格中的LookupID列显示基于LookupID的查找描述。如果我将combobox直接绑定到集合,我会得到一个错误,说“在Mystaff上找不到属性”,我想这是因为数据网格绑定到一个集合Mystaff,而Mystaff不知道LookupList。。。。因此,我将ViewModel作为静态资源传递,并将组合框绑定到LookupList。请看一下本文和“绑定嵌套控件时使用静态资源”一节,很抱歉,这并不能让事情变得更清楚。我认为最好是在问题中添加相关的ViewModel代码(这足以显示属性名称和类型,请不要执行PropertyChangedImplementation)。