How to use a struct value as dynamicresource?

尼龟 杰 190 Reputation points
2023-10-05T13:29:02.04+00:00

my app.xaml:

<Application x:Class="jngyy2024.App"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:JNGDataTypes.Music;assembly=JNGDataTypes"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:sysw="clr-namespace:System.Windows;assembly=PresentationFramework"
StartupUri="MainWindow.xaml">
         <Application.Resources>
                  <ResourceDictionary>
                      <ResourceDictionary.MergedDictionaries> 
                          <sysw:Thickness x:Key="AppBorderThickness">3</sysw:Thickness>      
                  </ResourceDictionary>     
         </Application.Resources> 
</Application>


I use it like this:

<jc:JCRevealButto BorderThickness="{DynamicResource AppBorderThickness}"></jc:JCRevealButton>
App app = App.Current as App;
app.Resources["AppBorderThickness"] = new Thickness(1);

But it didn't work, I seek it on internet , it is said that thickness is a struct-type value and dynamicresource can only be used on the ref-able-value, how can I fix it? Thanks for your answers!

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,706 Reputation points Microsoft External Staff
    2023-10-05T14:43:21.0666667+00:00

    Hi,@尼龟 杰. Welcome Microsoft Q&A.

    You could try the solution below to see if it is what you want. If so, you can refer to it and modify your code.

    app.xaml:

    <Application x:Class="WpfApp2.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:WpfApp2"
                  xmlns:sys="clr-namespace:System;assembly=mscorlib"
                 xmlns:sysw="clr-namespace:System.Windows;assembly=PresentationFramework"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
           
                    <ResourceDictionary>
                        <ResourceDictionary.MergedDictionaries>
                            <ResourceDictionary>
                                <sysw:Thickness x:Key="AppBorderThickness">3</sysw:Thickness>
                            </ResourceDictionary>
                        </ResourceDictionary.MergedDictionaries>
                    </ResourceDictionary>
                        
          
        </Application.Resources>
    </Application>
    

    MainWindow.xaml:

    <Window.Resources>
            <Style x:Key="btn" TargetType="{x:Type Button}">
                <Setter Property="BorderThickness" Value="{DynamicResource AppBorderThickness}" />
            </Style>
    
        </Window.Resources>
        <Grid>
          
            <Button Style="{StaticResource btn}" Width="200" Height="100" Content="My Button"/>
    
        </Grid>
    

    MainWindow.xaml.cs:

    
     public MainWindow()
            {
                InitializeComponent();
                App app = App.Current as App;
                app.Resources["AppBorderThickness"] = new Thickness(7);
    
            }
    
    

    The result:

    4


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.