根据SelectedDate更改DatePicker样式

发布于 2024-08-19 14:23:23 字数 1574 浏览 2 评论 0原文

第一个问题在这里。无论如何,事情是这样的:

我有一个带有大量 DatePicker 控件的 XAML Windows(来自 CodePlex 上的 WPFToolkit 的 DatePicker)。每个 DatePicker 的默认值都是 1/1/1990,如果没有选择其他日期,我希望(或者更确切地说是我的老板:-))以灰色斜体而不是黑色显示文本。因此,可以轻松查看哪些字段尚未输入日期。

这可能很简单,但我对 WPF/XAML 还很陌生。事实上,这是我使用它的第一步。

所以这就是我所拥有的并且实际上效果很好:

    <Style TargetType="{x:Type my:DatePicker}">
        <Style.Triggers>
            <Trigger Property="Text" Value="1/1/1990">
                <Setter Property="Foreground" Value="DarkGray"/>
                <Setter Property="ToolTip" Value="Please select a date"/>
                <Setter Property="FontStyle" Value="Italic"/>
            </Trigger>
        </Style.Triggers>
    </Style>

问题是,由于明显的本地化/区域设置问题,它并不适用于每台计算机。

所以我尝试了这个:

 <Style TargetType="{x:Type my:DatePicker}">
    <Style.Triggers>
        <Trigger Property="Text" Value="{Binding Source={x:Static p:Settings.Default}, Path=MinDate, Mode=TwoWay}">
            <Setter Property="Foreground" Value="DarkGray"/>
            <Setter Property="ToolTip" Value="Veuillez choisir une date"/>
            <Setter Property="FontStyle" Value="Italic"/>
        </Trigger>
    </Style.Triggers>
</Style>

注意触发器的“Value”属性的差异。这会产生以下错误:

无法在 “触发器”类型的“值”属性。一个 “绑定”只能设置在 a 的依赖属性 依赖对象。

我确实理解其含义,并且我理解为什么这不起作用。 (请注意,MinDate 的类型为 DateTime,值为 1/1/1990)

那么,如何在所有计算机上从第一个代码片段获得结果呢?

谢谢你的时间。

First question here. Anyway, here it goes:

I have a XAML Windows with a lot of DatePicker controls (DatePicker from the WPFToolkit on CodePlex). Every DatePicker has a default Value of 1/1/1990 and if no other date is selected, I want (or rather my boss :-) ) to display the Text in gray italic and not in Black. Thus, it makes it easy to see which fields the Date has not yet been entered.

This might be easy, but I'm quite new to WPF/XAML. In fact, these are my first steps using it.

So here is what I have and which works great actually:

    <Style TargetType="{x:Type my:DatePicker}">
        <Style.Triggers>
            <Trigger Property="Text" Value="1/1/1990">
                <Setter Property="Foreground" Value="DarkGray"/>
                <Setter Property="ToolTip" Value="Please select a date"/>
                <Setter Property="FontStyle" Value="Italic"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Problem is, it doesn't work on every machine because of localization/regional settings issues obviously.

So I tried this:

 <Style TargetType="{x:Type my:DatePicker}">
    <Style.Triggers>
        <Trigger Property="Text" Value="{Binding Source={x:Static p:Settings.Default}, Path=MinDate, Mode=TwoWay}">
            <Setter Property="Foreground" Value="DarkGray"/>
            <Setter Property="ToolTip" Value="Veuillez choisir une date"/>
            <Setter Property="FontStyle" Value="Italic"/>
        </Trigger>
    </Style.Triggers>
</Style>

Notice the difference in the "Value" property of the trigger. This yields following error:

A 'Binding' cannot be set on the
'Value' property of type 'Trigger'. A
'Binding' can only be set on a
DependencyProperty of a
DependencyObject.

Which I do understand the meaning of and I understand why this doesn't work. (Note that MinDate is of type DateTime with Value 1/1/1990)

So, how could I achieve the result from my first code snippet on all computers?

Thanks for the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-08-26 14:23:23

好的,对于那些碰巧遇到同样问题的人,这就是我最终所做的:

在 XAML 中:

<src:ConvertMinDate x:Key="ConvertMinDate"/>

<Style TargetType="{x:Type my:DatePicker}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=SelectedDate, RelativeSource={RelativeSource Self}, 
                Converter={StaticResource ConvertMinDate}}" Value="True">
                <Setter Property="Foreground" Value="DarkGray"/>
                <Setter Property="ToolTip" Value="Select a date"/>
                <Setter Property="FontStyle" Value="Italic"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

在代码隐藏中:

public class ConvertMinDate : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value == null)
        {
            return true;
        }
        else
        {
            DateTime date = Peche.Properties.Settings.Default.MinDate;
            if (value is DateTime)
            {
                date = (DateTime)value;
                return date == Peche.Properties.Settings.Default.MinDate;
            }
            else
            {
                return true;
            }
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MinDate 已在我的项目的“设置”部分中设置为 1990 年 1 月 1 日。

现在,如果 SelectedDate 为 null 或等于 MinDate,则转换后返回 true,从而触发 DataTrigger 并更改 DatePickerTextBox 的样式。

WPF 不是很棒吗? :-)

Ok, for those that happen to run into the same issue, here's what I finally did:

In XAML:

<src:ConvertMinDate x:Key="ConvertMinDate"/>

<Style TargetType="{x:Type my:DatePicker}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=SelectedDate, RelativeSource={RelativeSource Self}, 
                Converter={StaticResource ConvertMinDate}}" Value="True">
                <Setter Property="Foreground" Value="DarkGray"/>
                <Setter Property="ToolTip" Value="Select a date"/>
                <Setter Property="FontStyle" Value="Italic"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

In codebehind:

public class ConvertMinDate : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value == null)
        {
            return true;
        }
        else
        {
            DateTime date = Peche.Properties.Settings.Default.MinDate;
            if (value is DateTime)
            {
                date = (DateTime)value;
                return date == Peche.Properties.Settings.Default.MinDate;
            }
            else
            {
                return true;
            }
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MinDate has been set to 1st of january 1990 in the Settings section of my project.

So now, if the SelectedDate is either null or equals MinDate, the converted return true, thus triggering the DataTrigger and changing the style if the DatePickerTextBox.

Isn't WPF just great? :-)

一刻暧昧 2024-08-26 14:23:23

你不需要触发器。您只需更改模板的样式即可。 这篇文章对我很有帮助。

如果需要,您也可以设置当前日期。 这里是Datepicker 文档的链接。

You don't want a trigger. You can just change the style of the template. This article is what helped me.

Also You can set the current day if you want. Here is a link to the documentation for Datepicker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文