[英]How to get the reference of a TextBlock.Text property
好吧,我遇到了一个问题,我想获取TextBlock
拥有的文本,但是它无法正常工作。 我陷入了System.NullReferenceException
我已经建立了一个日历,将日期放置在TextBlock
现在我想获取此数据并将其与当前日期进行比较,然后突出显示该日期。
这是我使用的代码:
public class DateColorConvertor : IValueConverter
{
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return new object();
}
public object Convert(object sender, Type targetType, object parameter, string language)
{
string currentItem = null;
currentItem = (sender as TextBlock).Text;
if (currentItem.Equals(DateTime.Today.Date))
return new SolidColorBrush(Colors.Green);
else
{
return new SolidColorBrush(Colors.Red);
}
//throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
xaml:
<Grid Margin="10,102,10,298">
<GridView ItemsSource="{Binding Calendar.DateCollection}">
<GridView.ItemTemplate>
<DataTemplate>
<Grid x:Name="dateGrid" Background="Black" Width="50" Height="30">
<Grid.Resources>
<local:DateColorConvertor x:Key="DateColorConvertor"/>
</Grid.Resources>
<TextBlock x:Name="txtDate" Text="{Binding}" Foreground="{Binding Converter={StaticResource DateColorConvertor}}" VerticalAlignment="Center" HorizontalAlignment="Center" IsTapEnabled="True"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
不要那样做
IValueConverter
接收被绑定的值,仅此而已。
特别是,该参数称为value
,而不是sender
( source )。
您只需要将该值转换为DateTime
并直接使用即可。
另外,您可以返回Brushes.Red
。
您的Convert签名看起来与IValueConverter
接口定义的有所不同。
// Summary:
// Provides a way to apply custom logic to a binding.
public interface IValueConverter
{
// Summary:
// Converts a value.
//
// Parameters:
// value:
// The value produced by the binding source.
//
// targetType:
// The type of the binding target property.
//
// parameter:
// The converter parameter to use.
//
// culture:
// The culture to use in the converter.
//
// Returns:
// A converted value. If the method returns null, the valid null value is used.
object Convert(object value, Type targetType, object parameter, CultureInfo culture);
//
// Summary:
// Converts a value.
//
// Parameters:
// value:
// The value that is produced by the binding target.
//
// targetType:
// The type to convert to.
//
// parameter:
// The converter parameter to use.
//
// culture:
// The culture to use in the converter.
//
// Returns:
// A converted value. If the method returns null, the valid null value is used.
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
Convert方法应如下所示:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string currentItem = string.Format("{0}", value);
DateTime currentDate = DateTime.MinValue;
if (DateTime.TryParse(currentItem, out currentDate))
{
if (DateTime.Today.Equals(currentDate))
return new SolidColorBrush(Colors.Green);
}
return new SolidColorBrush(Colors.Red);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.