c# – WPF:如何更改TransitioningContentControl背景颜色?

我使用MahApps,使用此contentcontrol显示从文件中读取的许可证文本.

    <controls:TransitioningContentControl Transition="Down" Content="{Binding LicenseDocument}" FontFamily="{DynamicResource ContentFontFamily}" FontSize="{DynamicResource NormalFontSize}" Visibility="{Binding LicenseInfoVisibility}" TabIndex="0" Margin="0,-5,0,10"/>

public FrameworkElement LicenseDocument
{
  get;
  set;
}
public void InitDoc()
{
    try
    {
        const string licenseFullFile = @"License.rtf";
        using (
            MemoryStream stream = new MemoryStream(Encoding.Default.GetBytes(File.ReadAllText(licenseFullFile)))
            )
        {
            FlowDocument document = new FlowDocument();
            new TextRange(document.ContentStart, document.ContentEnd).Load(stream, DataFormats.Rtf);
            RichTextBox box = new RichTextBox(document)
            {
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                IsReadOnly = true
            };
            LicenseInfoVisibility = Visibility.Visible;
            this.LicenseDocument = box;
        }
    }
    catch (Exception xException)
    {
        Debug.WriteLine(xException);
    }
}

我使用时工作正常

<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

有我的resourcedictionary

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
        <!--<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />-->
        <!--<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />-->
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
    </ResourceDictionary.MergedDictionaries>

但是如果我使用BaseDark那么richtext背景和字体颜色是黑色的.

怎么了?

最佳答案 确保License.rtf的前景设置为“自动”,背景设置为“无颜色”.

XAML:

<Controls:MetroWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  xmlns:local="clr-namespace:MahApps.Metro.Application3" 
                  x:Class="MahApps.Metro.Application3.MainWindow"
                  Title="MainWindow"
                  Height="350"
                  Width="525">
<Controls:MetroWindow.DataContext>
    <local:MyViewModel/>
</Controls:MetroWindow.DataContext>

<Grid>
    <Controls:TransitioningContentControl Transition="Down" 
                                          Content="{Binding LicenseDocument}" 
                                          FontFamily="{DynamicResource ContentFontFamily}" 
                                          FontSize="{DynamicResource NormalFontSize}" 
                                          Visibility="{Binding LicenseInfoVisibility}" 
                                          TabIndex="0" Margin="0,-5,0,10"/>
</Grid>

Baselight来:

《c# – WPF:如何更改TransitioningContentControl背景颜色?》

BaseDark:

《c# – WPF:如何更改TransitioningContentControl背景颜色?》

点赞