Reference Resources for WPF Learning

Original: Reference Resources for WPF Learning

 

Copy code
<!- 
    Resource Foundation: Keep a series of objects and reuse them. 
    Advantages: 1. It has high efficiency, and can be tried in multiple places marked by defining resources, which makes the code streamlined.
          2. Maintainability. Creating resources is equivalent to creating constants in the program.
          3. Applicability. The specific information is separated from the rest of the application and placed in resources, which can be modified dynamically.     
-> 
<Window.Resources> 
    <!-The resource names at the same level cannot be the same- > 
    <ImageBrush x: Key = " TileBrush " TileMode = " Tile " ViewportUnits = " Absolute " Viewport = " 0 0 30 30 " ImageSource = " Image / Icon.png " > </ ImageBrush> 
</Window.Resources>
    <!-Each element has its own resources, each element can access the resources of itself and its parent tree, first access its own resources-> 
   <!-You can also place resources in the App. In the xmal file, because the App.xmal file is the top layer of the entire application-> <!-Static Resource (StaticResource) needs to be defined before reference-> <Button Margin = " 3 " Name = " button1 " FontSize = " 14 " Padding = " 5 " Background = " {StaticResource TileBrush} " > Static Resource </ Button> <Button Margin = " 3 " Name = " button2 " FontSize = "14" Padding="5" > <Button.Resources> <ImageBrush x: Key = " SelfTileBrush " TileMode = " FlipX " ViewportUnits = " Absolute " Viewport = " 0 0 30 30 " ImageSource = " Image / Icon.png " > </ ImageBrush> </ Button.Resources> <Button.Background> <!-Use your own resources need to refer to the resources after the defined resources-> <StaticResource ResourceKey = " SelfTileBrush " > </ StaticResource> </ Button.Background>Background> <Button.Content>Myself Resource</Button.Content> </Button> <Button Margin=" 3 " Name = " button3 " FontSize = " 14 " Padding = " 5 " Background = " {DynamicResource TileBrush} " > Dynamic Resource </ Button> <!- Put the following code in the click event, you can achieve dynamic modification Resources. However, the modified resource is only used for reference elements marked as DynamicResource. Here button3 changes, button1 does not change this .Resources [ " TileBrush " ] = new SolidColorBrush (Colors.SkyBlue); -> <Button Margin = " 3 " Name = "" 14 " Padding = " 5 " Click = " button4_Click " > Change Resource </ Button> <!-Reference system resources. You need to use dynamic resources to facilitate changes to the program after the system is modified. But its resource is static, so it needs to be marked as a static reference, and you need to add a Key- > <Button Margin = " 3 " Name = " button5 " FontSize = " 14 " Foreground = " {DynamicResource {x: Static SystemColors.WindowTextBrushKey}} " Padding = " 5 " > Change Resource </ Button> </ StackPanel>
Copy code

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12749275.html