美文网首页
使用Infragistics WPF controls遇到Bas

使用Infragistics WPF controls遇到Bas

作者: 勿念情 | 来源:发表于2019-06-25 18:00 被阅读0次

问题描述

WPF程序中使用Infragistics的控件,需要对控件进行全局的属性设置。一般来说,会想到使用BaseOn默认的Style来进行设置。但是Infragistics使用BaseOn会导致Style直接丢失。特别是在使用了其提供的主题的情况下(废话,不适用主题用它控件干嘛)。

问题分析

常规BaseOn写法

下面的代码对Button进行了背景色设置。由于没有指定Key,如果放在App.xaml中,会对全局程序生效。

<Style TargetType="Button" BaseOn="{StaticResource {x:Type Button}}">
    <Setter Property="Background" Value="Red"/>
</Style>

同样的写法,用于Infragistics控件

下面代码对ColorPicker是否显示RecentColor进行设置。

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ig="http://schemas.infragistics.com/xaml"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type ig:XamColorPicker}" BasedOn="{StaticResource {x:Type ig:XamColorPicker}}">
            <Setter Property="ShowRecentColorsPalette" Value="false"/>
        </Style>
    </Window.Resources>
    <Grid>
        <ig:XamColorPicker MinWidth="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Window>

当然,需要先在App.xaml.cs中设置主题。

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            ThemeManager.ApplicationTheme = new RoyalDarkTheme();
            base.OnStartup(e);
        }
    }
运行效果
预期结果

解决方案

ThemeManager中提供一个RegisterControl方法。我们需要在设置主题前对控件进行注册。修改App.xaml.cs。

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //Added this line
            ThemeManager.RegisterControl(typeof(XamColorPicker));
            ThemeManager.ApplicationTheme = new RoyalDarkTheme();
            base.OnStartup(e);
        }
    }

问题分析

To be continue.

相关文章

网友评论

      本文标题:使用Infragistics WPF controls遇到Bas

      本文链接:https://www.haomeiwen.com/subject/fopxcctx.html