Ну, я застрял в проблеме, где хочу получить текст, который имеет TextBlock
, но он не работает. Я застрял с System.NullReferenceException
Как получить ссылку на свойство TextBlock.Text
Я построил календарь, в котором даты размещены в 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>
Не делай этого. Вместо этого привяжите свойство и преобразуйте значение. – SLaks
может помочь мне больше в первый раз, используя C# – user3411961