Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent DatePicker and TimePicker fields from being overriden #8227

Closed
wants to merge 2 commits into from
Closed

Prevent DatePicker and TimePicker fields from being overriden #8227

wants to merge 2 commits into from

Conversation

CollinAlpert
Copy link
Contributor

What does the pull request do?

The text displayed for a DatePicker or TimePicker which has no value set is hardcoded. This PR only sets the respective TextBlock's text to the hardcoded value if another value has not yet been supplied (i.e. through styling).

What is the current behavior?

Currently any text set through styling gets overwritten.

What is the updated/expected behavior with this PR?

Adding the following style:

<Style Selector="DatePicker:hasnodate /template/ TextBlock#MonthText">
    <Setter Property="Text" Value="Monat" />
</Style>

This should cause an unset DatePicker to display the text "Monat" (German for "month") in the month's column.

@CollinAlpert CollinAlpert changed the title Prevent DatePicker and TimePicker fields from being overriden. Prevent DatePicker and TimePicker fields from being overriden May 30, 2022
_dayText!.Text = "day";
_monthText!.Text ??= "month";
_yearText!.Text ??= "year";
_dayText!.Text ??= "day";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if you clear the selected date (or time) after having it previously set since the TextBlock text won't be null. WinUI probably has localized strings here to prevent English only.
TextBlock.Text is also a DirectProperty, not a StyledProperty, so there's a chance the Style lookup in the OP will fail, and may not even work here to allow custom strings for :hasnodate (or :hasnotime)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, property localization for internal avalonia strings is something that we must have. It needs a proper design though.

@maxkatz6
Copy link
Member

<Style Selector="DatePicker:hasnodate /template/ TextBlock#MonthText">
    <Setter Property="Text" Value="Monat" />
</Style>

Note, that setters to a direct properties won't be allowed in the future. Still you can create edit this field directly without styles with some code. See #7982 . Although TextBlock.Text specifically might be styleable in the future.

@CollinAlpert
Copy link
Contributor Author

@maxkatz6 could you elaborate on that? How is it currently possible to change the value of the "month" column without styling?

@maxkatz6
Copy link
Member

@CollinAlpert sure:

myTimePicker.TemplateApplied += (s, a) =>
{
    var hourTextBlock = a.NameScope.Find<TextBlock>("HourTextBlock");
    hourTextBlock.Text = "час";
};

image

@maxkatz6
Copy link
Member

But unfortunately, it won't work if value will be null again. So it needs to be re-refreshed with a hack...:

private static void EnsureLocalization(TimePicker picker, string tempaltePartName, string value)
{
    picker.TemplateApplied += (s, a) =>
    {
        var textBlock = a.NameScope.Find<TextBlock>(tempaltePartName);
        textBlock.Text = value;

        textBlock.PropertyChanged += (s, a) =>
        {
            if (a.Property == TextBlock.TextProperty
                && picker.SelectedTime is null)
            {
                textBlock.Text = value;
            }
        };
    };
}

and from ctor:

EnsureLocalization(Picker, "HourTextBlock", "час");
EnsureLocalization(Picker, "MinuteTextBlock", "минуты");

@CollinAlpert
Copy link
Contributor Author

@maxkatz6 thanks. Although this implies a code-behind. Any way using MVVM?

@maxkatz6
Copy link
Member

@CollinAlpert that's purely View logic, so should work with MVVM as it is. Unless you want to read localized values from the ViewModel, but in this case I don't understand how styles would solve same problem.

@CollinAlpert CollinAlpert deleted the fix/dont_reset_picker_text branch May 31, 2022 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants