简体   繁体   中英

Making a textblock be visible if date is in the past WPF

I want a textblock to be visible if a date in a DatePicker is in the past.

View:

<DatePicker x:Name="TestInspector2Date" Text="{Binding SelectedTechnicianInfo.TestInspector2Date}"/>

<TextBlock x:Name="TrainingExpired" Text="Training Expired">
                        <TextBlock.Style>
                            <Style>
                                <Setter Property="TextBlock.Visibility" Value="Collapsed"/>
                                <Style.Triggers>
                                    <DataTrigger
                                        Binding="{Binding ElementName=TestInspector2Date, Path=Text, Converter={StaticResource DateConverter}}" Value="true">
                                        <Setter Property="TextBlock.Visibility" Value="Visible"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>

Converter:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date = DateTime.Parse(value.ToString());
            DateTime now = DateTime.Now;
            if (date.Date > now.Date)
                return true;
            else
                return false;
        }

I have tried adding the converter but I don't know how to bind it. If anyone has any ideas to fix this it would be great or if there is a better way to do it i am willing to change my ways.

Ok so here is my code that gets what you want I think.

Converter:

[ValueConversion(typeof(DateTime), typeof(Visibility))]
public class CurrentDateVisibilityConverter : IValueConverter
{
    public Visibility CurrentDateVisibility { get; set; } = Visibility.Visible;
    public Visibility PastDateVisibility { get; set; } = Visibility.Collapsed;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is DateTime)
        {
            DateTime inputDate= (DateTime)value;
            DateTime nowDate = DateTime.Now;
            if (inputDate.Date == nowDate.Date)
            {
                return CurrentDateVisibility;
            }
            else
            {
                return PastDateVisibility;
            }
        }
        else
        {
            return PastDateVisibility;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

}

Window XAML:

<Window x:Class="WpfApp9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp9"
        DataContext="{Binding RelativeSource={RelativeSource self}}"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:CurrentDateVisibilityConverter x:Key="CurrentDateVisibilityConverter" CurrentDateVisibility="Visible" PastDateVisibility="Hidden" />
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock Text="Date Current" Visibility="{Binding MyDate, Converter={StaticResource ResourceKey=CurrentDateVisibilityConverter}}" />
        <Button Name="btnSetDateToToday" Grid.Column="1" Click="btnSetDateToToday_Click">Set Date To Now</Button>
        <Button Name="btnSetDateToPast" Grid.Column="1" Grid.Row="1" Click="btnSetDateToPast_Click">Set Date To Past</Button>
    </Grid>
</Window>

Window Code Behind:

   public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static DependencyProperty MyDateProperty = DependencyProperty.Register("MyDate", typeof(DateTime), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(MyDate_Changed)));

        public DateTime MyDate  
        {
            get { return (DateTime)GetValue(MyDateProperty); }
            set { SetValue(MyDateProperty, value); }
        }


        private static void MyDate_Changed(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            MainWindow thisClass = (MainWindow)o;
            thisClass.SetMyDate();
        }

        private void SetMyDate()
        {
            //Put Instance MyDate Property Changed code here
        }

        private void btnSetDateToToday_Click(object sender, RoutedEventArgs e)
        {
            MyDate = DateTime.Now;
        }

        private void btnSetDateToPast_Click(object sender, RoutedEventArgs e)
        {
            MyDate = DateTime.Now.AddDays(-10);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM