MaterialDesignInXAML / MaterialDesignInXamlToolkit

Google's Material Design in XAML & WPF, for C# & VB.Net.
http://materialdesigninxaml.net
MIT License
14.82k stars 3.4k forks source link

Cannot find resource named 'MaterialDesignCharacterCounterTextBlock'. #3613

Open jamesport079 opened 1 week ago

jamesport079 commented 1 week ago

Bug explanation

Application is crashing when I'm trying to use a style based on MaterialDesignCharacterCounterTextBlock. All worked well in 5.0 and below so I don't really know what is changed.

Exception: Cannot find resource named 'MaterialDesignCharacterCounterTextBlock'. Resource names are case sensitive.

I tried merging MaterialDesignTheme.TextBox.xaml but still getting the same error. Error only appeared after I updated to 5.1

This is my style based on MaterialDesignCharacterCounterTextBlock...

<Style
    x:Key="ShowCounterOnKeyboardFocus"
    BasedOn="{StaticResource MaterialDesignCharacterCounterTextBlock}"
    TargetType="{x:Type TextBlock}">
    <Setter Property="Height" Value="0" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBox}}" Value="True">
            <Setter Property="Height" Value="{x:Static sys:Double.NaN}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Version

5.1.0

MichelMichels commented 6 days ago

The style has been put inside the style MaterialDesignTextBoxBase, which in turn made it unaccessible like a local style. I don't know if this specifically was intended by this PR: https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/pull/3461

@nicolaihenriksen can maybe answer this question better.

@jamesport079 for this moment, I'd write my own style with following content:

<Style x:Key="MaterialDesignCharacterCounterTextBlock"
             TargetType="TextBlock"
             BasedOn="{StaticResource {x:Type TextBlock}}">
        <Setter Property="FontSize" Value="10" />
        <Setter Property="Opacity" Value="0.56" />
        <Setter Property="Text">
          <Setter.Value>
            <MultiBinding StringFormat="{}{0} / {1}">
              <Binding Converter="{StaticResource StringLengthValueConverter}"
                       Path="Text"
                       RelativeSource="{RelativeSource FindAncestor,
                                                       AncestorType=TextBoxBase}" />
              <Binding Path="MaxLength" RelativeSource="{RelativeSource FindAncestor, AncestorType=TextBoxBase}" />
            </MultiBinding>
          </Setter.Value>
        </Setter>
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Visibility" Value="{Binding Path=(wpf:TextFieldAssist.CharacterCounterVisibility), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}}" />
      </Style>
jamesport079 commented 6 days ago

Thank you, @MichelMichels. I'll try that.

nicolaihenriksen commented 6 days ago

@jamesport079 @MichelMichels It was actually an intended change to move all TextBox related resources into a resource collection under the TextBox itself, rather than having a ton of resources (e.g. converters, data templates, utility styles, etc.) available via the app-level ResourceDictionary.

So I did not actually consider it a use case that consumers wanted to base a custom style on this one, but I am not against moving that style back out to the outer-most ResourceDictionary which would allow the desired "inhertance". @Keboo thoughts?

jamesport079 commented 6 days ago

I'm using that particular style to make counter appear when the TextBox has Keyboard focus. I don't like it that I have a bunch of TextBoxes having the counters showing when they're not in focus. It just makes the UI unnecessarily cluttered. So yeah, ideally the Style remains accessible unless there is a neater solution.

MichelMichels commented 6 days ago

A neater solution would be to implement such a setting, like ValidationAssist.OnlyShowOnFocus, but maybe on TextFieldAssist.OnlyShowCounterOnFocus.

jamesport079 commented 5 days ago

@MichelMichels yeah i thought about this way back. But not being able to access some styles in general irks me a little bit.

MichelMichels commented 5 days ago

@jamesport079 I understand your sentiment, but the style only makes sense in the context of a TextBox control as it binds to several properties of the TextBox. So I also understand why @nicolaihenriksen changed this to a local style.

@nicolaihenriksen we could however revert the change and maybe propose the change for v6 as this seems to be a breaking change for @jamesport079 (and maybe other users)?