如何使用xamlreader加载样式

时间:2013-08-22 09:54:56

标签: wpf styles xamlreader

如何使用XamlReader.Load()

从xaml下方加载样式
<Window
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
        xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
>
<ResourceDictionary>
        <Style x:Key="LastRowHighlighted"
        BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}"
               TargetType="{x:Type dxg:GridRowContent}">
        </Style>
</ResourceDictionary>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

public void LoadStyle(string fileName)
{
    if (File.Exists(fileName))
    {
        try
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.Open, 
FileAccess.Read, FileShare.Read))
            {
                ResourceDictionary resourceDictionary = (ResourceDictionary)XamlReader.
Load(fileStream);
                Resources.MergedDictionaries.Clear(); // optional
                Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }
        catch { }
    }
}