x:Bind DataGridTemplateColumn.CellTemplate 中的 DataTemplate 未显示内容
x:Bind DataTemplate in DataGridTemplateColumn.CellTemplate is not displaying content
根据 this windows-toolkit issue 的响应,我正在使用 x:Bind 将 AlertEntry 的 ObservableCollection 的元素绑定到 DataGridColumn 单元格。我的XAML如下:
<controls:DataGrid ItemsSource="{x:Bind ViewModel.Alerts, Mode=OneWay}" AutoGenerateColumns="True" IsReadOnly="True">
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn Header="Time" >
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="ace:AlertEntry">
<TextBlock Text="{x:Bind Timestamp, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{H:mm:ss}'}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
还有我的 AlertEntry class:
public class AlertEntry
{
public DateTime Timestamp;
public ACEEnums.AlertLevel Level;
public ACEEnums.AlertType Type;
public string Info;
public AlertEntry(ACEEnums.AlertLevel level, ACEEnums.AlertType type, string info = "")
{
Timestamp = DateTime.Now;
Level = level;
Type = type;
Info = info;
}
}
当元素添加到 ViewModel.Alerts 时,我可以看到可突出显示的行已添加到 DataGrid,但它们不显示任何内容。当我删除绑定并添加固定文本值时,每次添加行时它都会正确显示该值。
ViewModel.Alerts 中的 AlertEntry 项正确包含数据。
我已确认 StringFormatConverter 在其他绑定中有效。事实上,StringFormatConverter 从未被调用过。
我正在使用 MVVM-Light 和 UWP。
谢谢!
为了测试,问题可能出现在你的StringFormatConverter
,TextBlock
文本属性只允许字符串值,所以我们需要return字符串类型的值在Convert
方法。
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
if (parameter == null)
return value;
var dt = (DateTime)value;
return dt.ToString((string)parameter);
}
和x:Bind支持绑定function,你可以尝试用Text="{x:Bind Timestamp.ToString()}"
验证上面的
我的 DataGrid 是 StackPanel 的一个元素,我将我的转换器声明为 StackPanel 的静态资源,如下所示:
<StackPanel.Resources>
<helper:StringFormatConverter x:Key="StringFormatConverter" />
<helper:AlertToString x:Key="AlertToString" />
</StackPanel.Resources>
我将转换器移动为整个页面的资源,然后它们开始工作。
<Page.Resources>
<helper:StringFormatConverter x:Key="StringFormatConverter" />
<helper:AlertToString x:Key="AlertToString" />
</Page.Resources>
根据 this windows-toolkit issue 的响应,我正在使用 x:Bind 将 AlertEntry 的 ObservableCollection 的元素绑定到 DataGridColumn 单元格。我的XAML如下:
<controls:DataGrid ItemsSource="{x:Bind ViewModel.Alerts, Mode=OneWay}" AutoGenerateColumns="True" IsReadOnly="True">
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn Header="Time" >
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="ace:AlertEntry">
<TextBlock Text="{x:Bind Timestamp, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{H:mm:ss}'}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
还有我的 AlertEntry class:
public class AlertEntry
{
public DateTime Timestamp;
public ACEEnums.AlertLevel Level;
public ACEEnums.AlertType Type;
public string Info;
public AlertEntry(ACEEnums.AlertLevel level, ACEEnums.AlertType type, string info = "")
{
Timestamp = DateTime.Now;
Level = level;
Type = type;
Info = info;
}
}
当元素添加到 ViewModel.Alerts 时,我可以看到可突出显示的行已添加到 DataGrid,但它们不显示任何内容。当我删除绑定并添加固定文本值时,每次添加行时它都会正确显示该值。
ViewModel.Alerts 中的 AlertEntry 项正确包含数据。
我已确认 StringFormatConverter 在其他绑定中有效。事实上,StringFormatConverter 从未被调用过。
我正在使用 MVVM-Light 和 UWP。
谢谢!
为了测试,问题可能出现在你的StringFormatConverter
,TextBlock
文本属性只允许字符串值,所以我们需要return字符串类型的值在Convert
方法。
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
if (parameter == null)
return value;
var dt = (DateTime)value;
return dt.ToString((string)parameter);
}
和x:Bind支持绑定function,你可以尝试用Text="{x:Bind Timestamp.ToString()}"
验证上面的
我的 DataGrid 是 StackPanel 的一个元素,我将我的转换器声明为 StackPanel 的静态资源,如下所示:
<StackPanel.Resources>
<helper:StringFormatConverter x:Key="StringFormatConverter" />
<helper:AlertToString x:Key="AlertToString" />
</StackPanel.Resources>
我将转换器移动为整个页面的资源,然后它们开始工作。
<Page.Resources>
<helper:StringFormatConverter x:Key="StringFormatConverter" />
<helper:AlertToString x:Key="AlertToString" />
</Page.Resources>