C# 在DataGridTemplateColumn中使用TemplateSelector时绑定不再工作
我有以下(工作)C# 在DataGridTemplateColumn中使用TemplateSelector时绑定不再工作,c#,wpf,xaml,datatemplate,datatemplateselector,C#,Wpf,Xaml,Datatemplate,Datatemplateselector,我有以下(工作)XAML定义: <DataGridTemplateColumn Header="Station & Programm" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ListBox
XAML
定义:
<DataGridTemplateColumn Header="Station & Programm" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox
ItemsSource="{Binding Targets}"
Style="{StaticResource ListBoxTransparentStyle}"
VerticalAlignment="Center"
IsHitTestVisible="False"> <!--Disable Selection-->
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Text="{Binding Station}"
Height="Auto"/>
<TextBlock
Grid.Column="2"
Text="{Binding Program}"
Height="Auto"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
之后,我将代码更改如下:
<ControlTemplate>
<ControlTemplate.Resources>
<DataTemplate x:Key="StationProgramView" x:Shared="True">
<ListBox
ItemsSource="{Binding Targets}"
Style="{StaticResource ListBoxTransparentStyle}"
VerticalAlignment="Center"
IsHitTestVisible="False"> <!--Disable Selection-->
<ListBox.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<controls:HtTextfeld
Grid.Column="0"
Text="{Binding Station}"
DisableTranslation="True"
Height="Auto"/>
<controls:HtTextfeld
Grid.Column="2"
Text="{Binding Program}"
DisableTranslation="True"
Height="Auto"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
<DataTemplate x:Key="StationProgramEdit" x:Shared="True">
<TextBlock Text="Hello World"></TextBlock>
</DataTemplate>
<recipeControls:ZlsRouteEditorDataTemplateSelector x:Key="StationProgramTemplateSelector" ViewDataTemplate="{StaticResource StationProgramView}" EditDataTemplate="{StaticResource StationProgramEdit}"/>
已正确选择
DataTemplate
s,但数据绑定不再工作。尝试设置列的CellTemplateSelector
属性,而不是将CellTemplate
设置为ContentControl
:
<DataGridTemplateColumn Header="Station & Programm" Width="*" CellTemplateSelector="{StaticResource StationProgramTemplateSelector}" />
ContentControl
的ContentTemplate
中根元素的DataContext
是相同ContentControl
的内容
我已经更改了ZlsRouteEditorDataTemplateSelector
类,该类能够查找我的特定控件(ZlsRouteEditor
)并获取模式
值
ZlsRouteEditorDataTemplateSelector
为什么要将DataTemplate的x:Shared属性设置为true?我想,如果您对多个实例(对于DataGrid
中的每一行)使用DataTemplate
,请设置此标志。感谢您的回复。我已经尝试过这个解决方案,但是如何将我的Mode
属性(Content=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=recipeControls:ZlsRouteEditor},Path=Mode}”/>
)传递到StationProgramTemplateSelector
?根据其值,选择数据模板
。-自己回答->在类中为其设置另一个属性
!无论如何谢谢你!在类中为其设置另一个属性!不可能,因为无法绑定属性。绑定只能在DependencyObject
s上进行,而DataTemplateSelector
则不能。如果我使用解决方法来使用ContentControl
(),我将遇到与我所问问题相同的问题!
<DataGridTemplateColumn Header="Station & Programm" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl
ContentTemplateSelector="{StaticResource StationProgramTemplateSelector}"
Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=recipeControls:ZlsRouteEditor}, Path=Mode}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Station & Programm" Width="*" CellTemplateSelector="{StaticResource StationProgramTemplateSelector}" />
public class ZlsRouteEditorDataTemplateSelector : DataTemplateSelector
{
public DataTemplate ViewDataTemplate { get;set; }
public DataTemplate EditDataTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is ERouteEditor e)
return _GetTemplate(e);
ZlsRouteEditor parent = container.TryFindParent<ZlsRouteEditor>();
if (parent != null)
return _GetTemplate(parent.Mode);
return base.SelectTemplate(item, container);
}
private DataTemplate _GetTemplate(ERouteEditor e)
{
switch (e)
{
case ERouteEditor.View:
return ViewDataTemplate;
case ERouteEditor.Edit:
return EditDataTemplate;
default:
throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// Finds a parent of a given item on the visual tree.
/// </summary>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="child">A direct or indirect child of the
/// queried item.</param>
/// <returns>The first parent item that matches the submitted
/// type parameter. If not matching item can be found, a null
/// reference is being returned.</returns>
public static T TryFindParent<T>(this DependencyObject child) where T : DependencyObject
{
//get parent item
DependencyObject parentObject = GetParentObject(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
T parent = parentObject as T;
if (parent != null)
{
return parent;
}
else
{
//use recursion to proceed with next level
return TryFindParent<T>(parentObject);
}
}
<DataGridTemplateColumn Header="Station & Programm" Width="*" CellTemplateSelector="{StaticResource StationProgramTemplateSelector}"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<recipeControls:ZlsRouteEditor Height="570" Margin="5" Mode="View"/>
<recipeControls:ZlsRouteEditor Height="570" Margin="5" Mode="Edit"/>
</StackPanel>