编辑:尝试实现转换器时 Gettign Xaml 解析错误
错误:在“System.Windows.Markup.StaticResourceHolder”上提供值引发异常。
<DataTemplate>
<myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148"
Margin="10,0,5,0"
Style="{DynamicResource ButtonStyleRegistration}"
Click="RegistrationButton_Click"
Title="{Binding Title}"
Oorzaak="{Binding Oorzaak}"
DuurStilstand="{Binding DuurStilstand,
Converter={StaticResource DateTimeConvertor},
ConverterParameter=\{0:t\}}"
BeginStilstand="{Binding BeginStilstand}"
/>
</DataTemplate>
值转换器:
public class DateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Retrieve the format string and use it to format the value.
string formatString = parameter as string;
if (!string.IsNullOrEmpty(formatString))
{
return string.Format(culture, formatString, value);
}
// If the format string is null or empty, simply call ToString()
// on the value.
return value.ToString();
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
目前我有一个字符串依赖属性,它使用绑定并正确显示所有内容。虽然这个属性应该是 DateTime 。我在我的应用程序中使用字符串进行计算,这应该使用一个 DateTime 值来代替。
当我尝试将其更改为 DateTime 时,除了绑定错误之外什么也没有得到。在属性为字符串类型之前。
public static readonly DependencyProperty DuurStilstandProperty =
DependencyProperty.Register("DuurStilstand", typeof(DateTime), typeof(RegistrationButton), new UIPropertyMetadata(""));
public DateTime DuurStilstand
{
get { return (DateTime)GetValue(DuurStilstandProperty); }
set { SetValue(DuurStilstandProperty, value); }
}
XAML 绑定:
<TextBlock x:Name="tbDuurStilstand" TextWrapping="Wrap"
Text="{Binding DuurStilstand, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="7.5,5,0,0" Height="24.8266666666667"/>
<ListBox.ItemTemplate>
<DataTemplate>
<myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148"
Margin="10,0,5,0"
Style="{DynamicResource ButtonStyleRegistration}"
Click="RegistrationButton_Click"
Title="{Binding Title}"
Oorzaak="{Binding Oorzaak}"
DuurStilstand="{Binding DuurStilstand}"
BeginStilstand="{Binding BeginStilstand}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
此处显示错误 (XamlParseException):
<myClasses:RegistrationButton x:Name="btnTestRegistration" Content="Test Registratie" HorizontalAlignment="Left" Margin="16.8,0,0,118.14"
VerticalAlignment="Bottom" Width="119.2" Height="30.2" FontSize="18.667" Click="btnTestRegistration_Click" Style="{DynamicResource ButtonStyleRegistration}" />
为什么我无法将其更改为 DateTime。
文本块需要一个字符串而不是它当前正在获取的 DateTime。当内容放置在文本块的 .text 属性中时,我如何确保它将 DateTime 转换为字符串?
最好的问候,彼得P