【发布时间】:2017-08-21 23:31:21
【问题描述】:
我尝试对 DataGrid 中的列使用具有 DependencyProperty 绑定的自定义控件。一切正常,直到我选择了一行。
我的自定义控件 ShiftControl.xaml:
<UserControl x:Class="xRoster.UserControls.ShiftControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:xRoster.UserControls">
<TextBlock x:Name="tbShift"
Text="{Binding Path=Shift}"
TextWrapping="Wrap"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</UserControl>
我的 ShiftControl.xaml.cs 代码隐藏:
public partial class ShiftControl : UserControl
{
public ShiftControl()
{
InitializeComponent();
tbShift.DataContext = this;
}
public static readonly DependencyProperty ShiftProperty =
DependencyProperty.Register(
"Shift", typeof(string), typeof(ShiftControl));
public string Shift
{
get { return (string)GetValue(ShiftProperty); }
set { SetValue(ShiftProperty, value); }
}
}
我在 DataGrid window.xaml 中使用 ShiftControl 的代码:
<UserControl.Resources>
<DataTemplate x:Key="day1Column">
<uc:ShiftControl Shift="{Binding Day1Shift.Display}"/>
</DataTemplate>
</UserControl.Resources>
<DataGrid ItemsSource="{Binding Employees}"
AutoGenerateColumns="False"
HeadersVisibility="Column"
RowHeight="50">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Montag"
HeaderStyle="{StaticResource columnHeaderStyle}"
CellTemplate="{StaticResource day1Column}"
CellEditingTemplate="{StaticResource day1Column}"/>
</DataGrid.Columns>
</DataGrid>
正如我所说,我的 ShiftControl 的绑定仅在未选择放置它的行时才起作用。
有什么想法吗? 提前致谢
【问题讨论】:
-
这似乎只是 TextBlocks 的问题。使用相同的 Binding 的标签、按钮或文本框没有问题..
-
见stackoverflow.com/questions/9035878/…。没有好的minimal reproducible example 是不可能确定的,但根据您的评论,您的问题似乎与那个问题重复。
标签: c# wpf xaml datagrid wpf-controls