I'm writing a module for a prism application that we do not control. The requirement is to show a web browser control in one of the regions. Unfortunately, each window derives from a CustomWindow class, that has AllowsTransparency=true
. Having AllowTransparency=true
prevents the WebBrowser control from displaying.
I can right click and hover over the control and know there is a web page loaded (google), so I'm nearly certain that the problem I'm facing has to do with transparency and win32 controls (of which the WebBrowser is a wrapped win32 control to my knowledge).
So, I've decided that my only course of action is to try and override the Window style, to turn AllowTransparency off.
This is the offending style (using Reflector to browse the baml):
<Style x:Key="{x:Type local:CustomWindow}" TargetType="{x:Type local:CustomWindow}">
<Setter Property="AllowsTransparency" Value="true" />
...
</Style>
And this is how I'm trying to remove the style开发者_JS百科:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Vendor.App.WPFCommon.Controls;assembly=Vendor.App.WPFCommon">
<Style TargetType="{x:Type Controls:CustomWindow}">
<Setter Property="AllowsTransparency" Value="false" />
</Style>
</ResourceDictionary>
private void LoadThemeOverrides()
{
var assemblyName = System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.Name);
var overrides = new Uri(string.Format("{0};component/themes/overrides.xaml", assemblyName), UriKind.Relative);
var themeManager = _container.Resolve<IThemeManager>();
foreach (var theme in themeManager.ThemeCollection)
theme.Sources.Add(overrides);
var rd = new ResourceDictionary {Source = overrides};
Application.Current.Resources.MergedDictionaries.Add(rd);
themeManager.ChangeTheme(themeManager.CurrentTheme);
}
The ResourceDictionary is being loaded correctly, so it's not the URI that is the problem. I debugged the rd
, and I can see my style in there.
The above piece of code is run between the login window validating user/password, and the main application window being displayed. They are two different windows, however, they both derive from CustomWindow.
Using WPF Inspector, I can see that the CustomWindows still have AllowTransparency set to true. Am I able to override this style at all? Or am I attempting to do this incorrectly?
With windows, setting implicit style will not work in every situation. You have to give a key to the style and find a way to set the style explicitly on the window that requires that.
Using ResourceKey
might help, depending upon your architecture.
精彩评论