Marvin's Blog【程式人生】

Ability will never catch up with the demand for it

24 Oct 2020


3 Techniques you can use to make your data templates dynamic

  • Value Converters
  • Visual State Manager
  • Data Template Selectors

Overriding Resources from Generic.xaml in UWP Applications


I don't think WPF application will do what you want. Actually, it's related to the resource lookup behavior. The generic.xaml in theme folder will be always the first place to lookup for this scenario. 

How do I reference a StaticResource in code-behind?


foo2.Style = this.Resources["NavigationBackButtonNormalStyle"] as Style;


play_btn.Style = App.Current.Resources["btnplay"] as Style;

参考Why am I not able to use FindResource() in Windows phone programming?


对于UWP来说,可能需要自己定义TryFindResource(参考How to set ItemsSource property programmatically?):

public static class FrameworkElementExtensions
    public static object TryFindResource(this FrameworkElement element, object resourceKey)
        var currentElement = element;

        while (currentElement != null)
            var resource = currentElement.Resources[resourceKey];
            if (resource != null)
                return resource;

            currentElement = currentElement.Parent as FrameworkElement;

        return Application.Current.Resources[resourceKey];

// Or, the recursive version of TryFindResource method as suggested by @Default:

public static object TryFindResource(this FrameworkElement element, object resourceKey)
    if (element == null)
        return Application.Current.Resources[resourceKey];

    var resource = element.Resources[resourceKey];
    if (resource != null)
        return resource;
    return TryFindResource(element.Parent, resourceKey);

UWP ResourceDictionary Style Error: The parameter is incorrect

Custom types used as resources can’t have the UIElement class in their inheritance, because a UIElement can never be shareable (it’s always intended to represent exactly one UI element that exists at one position in the object graph of your runtime app).

<Style x:Key="ME_BASE_AppbarButtonSaveStyle" TargetType="AppBarButton">
    <Setter Property="Label" Value="Save" />
    <Setter Property="ToolTipService.ToolTip" Value="Save" />
    <Setter Property="ContentTemplate">
                <FontIcon FontFamily="Segoe MDL2 Assets"
              Glyph="&#xE105;" />

Can you define multiple TargetTypes for one XAML style?


<Style x:Key="Highlight" TargetType="{x:Type Control}">
    <Setter Property="Foreground" Value="Red"/>


<Button Style="{StaticResource Highlight}" Content="Test"/>
<TextBox Style="{StaticResource Highlight}" Text="Test"/>
<CheckBox Style="{StaticResource Highlight}" Content="Test"/>

WPF Databinding the negative of a boolean

  • Use a DataTrigger
  • Implement a iValueConverter
                <DataTrigger Binding="{Binding ElementName=cb, Path=IsChecked}" Value="True">
                    <Setter Property="TextBox.IsEnabled" Value="False"/>
                <DataTrigger Binding="{Binding ElementName=cb, Path=IsChecked}" Value="False">
                    <Setter Property="TextBox.IsEnabled" Value="True"/>

Conditional XAML

Conditional XAML provides a way to use the ApiInformation.IsApiContractPresent method in XAML markup. This lets you set properties and instantiate objects in markup based on the presence of an API without needing to use code behind. It selectively parses elements or attributes to determine whether they will be available at runtime. Conditional statements are evaluated at runtime, and elements qualified with a conditional XAML tag are parsed if they evaluate to true; otherwise, they are ignored.

WPF ControlTemplate breaks style

WPF considers ControlTemplates to be a boundry, and will not apply implicit styles (styles without an x:Key) inside of templates. But there is one exception to this rule: anything that inherits from Control will apply implicit styles.

One way to get around this is to add an implicit style in the ControlTemplate.Resources that is BasedOn the existing implicit TextBlock style:

        <Style TargetType="{x:Type TextBlock}" 
               BasedOn="{StaticResource {x:Type TextBlock}}" />

Place the implicit style in <Application.Resources>. Styles placed here will apply to your entire application, regardless of template boundaries. Be careful with this though, as it will apply the style to TextBlocks inside of other controls as well,

    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Green" />

Use a Label instead of a TextBlock since it’s inherited from Control, so will apply implicit Styles defined outside the ControlTemplate

Give the base style an x:Key and use it as the base style for an implicit TextBlock styles inside the ControlTemplate. It’s pretty much the same as the top solution, however it’s used for base styles that have an x:Key attribute

<Style x:Key="BaseTextBlockStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="Green" />


    <Style TargetType="{x:Type TextBlock}" 
        BasedOn="{StaticResource BaseTextBlockStyle}" />

UWP Changing style of AppBarButton breaks the dynamic overflow&rsquo;s automatic styling

As you’ve known, while an AppBarButton is added into SecondaryCommands, it will display in Popup. And if you check with Live Visual Tree in Visual Studio, you will find the AppBarButton is added in to a CommandBarOverflowPresenter named “SecondaryItemsControl”.





<Image Source="Assets/Favorite.png"
       Visibility="{x:Bind (Visibility)ViewModel.IsFavorited}" />


<ListView ItemsSource="{x:Bind ViewModel.Samples}">
            <ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView" />

来源Two hidden gems in the Universal Windows Platform