index > Windows Presentation Foundation ("Avalon") > Override properties set manualy with resourcedictionary.

Override properties set manualy with resourcedictionary.

Look at this code:

mediaplayer.Height = 500;

mediaplayer.Width = 500;

Stream s = File.Open("myskin.xaml", FileMode.Open);

ResourceDictionary rd = (ResourceDictionary)XamlReader.Load(s);

s.Close();

this.Resources = rd;

Skin.xaml contains this:


<Style x:Key="mediaplayer" TargetType="{x:Type Grid}">

<Setter Property="Width" Value="200" />

<Setter Property="Height" Value="200" />

</Style>

Why will the mediaplayer get the height/width 500 and not 200? I want the last assigned value to have priority over the old one, regardless if it's set manually or through a ResourceDictionary.

Chimme

Hello,

I'm new to wpf but I recently worked with skins...

Do you have a grid that looks something like this:

<Grid Style="{StaticResource mediaplayer}">

....

</Grid>

If so, I think you would need to change to;

<Grid Style="{DynamicResource mediaplayer}">

....

</Grid>

That way when you load your skin the skin's properties will kick in dynamically.

One other thing...

If you have multiple ResourceDictionaries remember to merge them...

Houman

Houman b

I'm using a DynamicResource so that's not the problem, thanks anyway.

Loading a resourcedictonary that says that the width is 1000 and then loading another RD that says that the width is 200 works, then the Grid will first get the widht 1000 and when you load the second RD it will get 200. But if you set the width manually by code it will override all other settings and RDs.

Chimme

Solved the problem by running this code before i load the resourcedictionary.

mediaplayer.ClearValue(Grid.WidthProperty);

mediaplayer.ClearValue(Grid.HeightProperty);

If someone has any better sollution i'm still happy to hear it.

Chimme

Calling ClearValue there seems like a perfectly reasonable solution. Any locally set value is going to beat all ResourceDictionary values except animations, and the best way to get rid of something locally set is ClearValue. Then what happens with your styles and RDs can jump into action. Other trickery like trying to bind the value still won't help, if the goal is to have an RD value beat a local value. However, I'm curious how you are getting the local value in the first place, and if it's necessary. If you could have those initial height/width values in styles, you could just be swapping styles, which seems like a much more natural way to work with skins.

Wolf Schmidt - MSFT

Ok, thanks.

I also noticed that animations have even higher priority than manually set properties. If you have animated an object somewhere, not even ClearValue helps. So i have to set the property to the current position manually in the animationcomplete-event and then ClearValue works again.
This is a pretty ugly sollution but it works fine because im only using this sollution once in my project.

 

The reason why i do this way?

I'm making a mediaplayer and when i play the movies in fullscreen i want the aspectratio to be correct, this has to be calculated manually cause i have placed everything in a ViewBox with fixed height/width to allow easier skinning for all resolutions. If you'r monitors aspectratio isn't the same as the skins, the movie will get a strange stretch even if you use StretchUniform on the MediaElement.

So, when you enter fullscreenmode i override all skinsettings and calculate the height/width manually and when you switch back from fullscreen the skins get priority again. Here the aspectratio for the movie isn't that important, it's just a small previewwindow so i don't think anyone will watch an entire movie here.

Chimme
reply 6

You can use google to search for other answers

 

More Articles

• ContextMenu : Cannot find TargetName in scope of System.Windows.C...
• Ivalidate LayoutTransformation
• Example: DesktopSidebar Progress (built with July CTP)
• How to assign database/source of ListView programmatically in WPF...
• ControlTemplate.Storyboards
• Animating a canvas?
• Passing additional parameters to the DataContext
• Dockable windows tool for WPF
• browser app questions
• Smart rendering of large amounts of geometries
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Help search problem
• x:Static works as element but not as att
• Post file in WinFX
• Opening Window in WPF application
• ListView GridLines
• Overlapping elements over a FlowDocument
• Canvas.Get...Whatever
• Populating TreeView with xml file in c#?
• Conversion Validation order
• Remove the Navigation Bar from xaml Page
• Triggers and MultTriggers problems
• XamlWriter : Does NameScope.RegisterName
• Set Up project is not working properly
• How to Bind to an ADO.NET Data Source.
• Storyboard pause behavior

Hot Articles

• How To Get Element within a Listview Cel
• Not able to get "A Visual" fro
• how to add text to a 3d shape?
• Select a cell in a ListView
• Measure distance to an item inside a sta
• ScrollViewer with non scrolling area (fi
• What's the recommended way to save an bi
• Binding a resource object TO a dependenc
• PrintTicket.PageOrientation does not hav
• XamlReader? Codebehind classes don't work?
• About Window.AllowsTransparency......
• Catching Exception at App Level
• powerpoint slide viewer control.....
• XAMLPad for June CTP neeeeeeded
• Measuring "rich text"

Recommend Articles

• Change the image dynamically from the St
• Xml databinding drops when launching app
• Viewport3D and real time
• Tearing in WPF
• Activated event raised on minimize
• I Want to know that how to Add Subitems
• Changing default editor for XAML
• RichTextBox/TextBox Autosize in a StackP
• Drawing high number of buttons
• How to extend functionality using custom
• Very slow grouping
• [WPF] Performance problems on Windows Vi
• DATA BINDING: Problems attaching a Valid
• Xaml Help with ResourceDictonary (MenuIt
• Applying a style to the child items of a