2016-07-07 9 views
0

如何将Color(Dark/Light)应用于UserControl?我成功检索并应用口音颜色,但我无法弄清楚如何应用黑/浅背景颜色。默认情况下,我的UserControl的背景是透明的。ModernUI:将当前主题应用于UserControl BackGround

这是我如何应用口音颜色(这工作):

<UserControl x:Class="GenkaiModern.Pages.DetailView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:GenkaiModern.Pages" 
       xmlns:mui="http://firstfloorsoftware.com/ModernUI" 
      mc:Ignorable="d" 

      d:DesignHeight="399.533" d:DesignWidth="751.402" Width="720" Height="350" Background="{DynamicResource Accent}" > 
</UserControl> 

它可能很简单,但我无法弄清楚如何使用这些资源属性。

现在这里的主窗口至极有应用。查阅全文给它的底色主题(我甚至不知道如何)

<mui:ModernWindow 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mui="http://firstfloorsoftware.com/ModernUI" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="GenkaiModern.MainWindow" 
     Title="Genkai Client" IsTitleVisible="True" 
     LogoData="F1 M 24.9015,43.0378L 25.0963,43.4298C 26.1685,49.5853 31.5377,54.2651 38,54.2651C 44.4623,54.2651 49.8315,49.5854 50.9037,43.4299L 51.0985,43.0379C 51.0985,40.7643 52.6921,39.2955 54.9656,39.2955C 56.9428,39.2955 58.1863,41.1792 58.5833,43.0379C 57.6384,52.7654 47.9756,61.75 38,61.75C 28.0244,61.75 18.3616,52.7654 17.4167,43.0378C 17.8137,41.1792 19.0572,39.2954 21.0344,39.2954C 23.3079,39.2954 24.9015,40.7643 24.9015,43.0378 Z M 26.7727,20.5833C 29.8731,20.5833 32.3864,23.0966 32.3864,26.197C 32.3864,29.2973 29.8731,31.8106 26.7727,31.8106C 23.6724,31.8106 21.1591,29.2973 21.1591,26.197C 21.1591,23.0966 23.6724,20.5833 26.7727,20.5833 Z M 49.2273,20.5833C 52.3276,20.5833 54.8409,23.0966 54.8409,26.197C 54.8409,29.2973 52.3276,31.8106 49.2273,31.8106C 46.127,31.8106 43.6136,29.2973 43.6136,26.197C 43.6136,23.0966 46.127,20.5833 49.2273,20.5833 Z"   
     ContentSource="/Pages/Home.xaml" d:DesignWidth="1070.985" d:DesignHeight="664.851"> 

    <mui:ModernWindow.MenuLinkGroups> 
     <mui:LinkGroup DisplayName="Administration" GroupKey="Administration"> 
      <mui:LinkGroup.Links> 
       <mui:Link DisplayName="Machine" Source="/Pages/NewComputerView.xaml" /> 
       <mui:Link DisplayName="Ecrans" Source="/Pages/Home.xaml"/> 
       <mui:Link DisplayName="Imprimantes" Source="/Pages/Home.xaml" /> 
      </mui:LinkGroup.Links> 
     </mui:LinkGroup> 
     <mui:LinkGroup DisplayName="settings" GroupKey="settings"> 
      <mui:LinkGroup.Links> 
       <mui:Link DisplayName="Application" Source="/Pages/SettingsPage.xaml" /> 
      </mui:LinkGroup.Links> 
     </mui:LinkGroup> 
     <mui:LinkGroup DisplayName="Login" GroupKey="Login"> 
      <mui:LinkGroup.Links> 
       <mui:Link DisplayName="Login" Source="/Pages/AutentificationView.xaml" /> 
      </mui:LinkGroup.Links> 
     </mui:LinkGroup> 
    </mui:ModernWindow.MenuLinkGroups> 

    <mui:ModernWindow.TitleLinks> 
     <mui:Link DisplayName="Administration" Source="/Pages/NewComputerView.xaml" /> 
     <mui:Link DisplayName="Login" Source="/Pages/AutentificationView.xaml" /> 
     <mui:Link DisplayName="settings" Source="/Pages/SettingsPage.xaml" /> 
     <mui:Link DisplayName="help" Source="https://github.com/firstfloorsoftware/mui" /> 
    </mui:ModernWindow.TitleLinks> 

</mui:ModernWindow> 

从我的理解,因为在我的usercontrole字体,如果采取modernui的字体我的主题背景不是透明的,它会产生严重的颜色,黑暗/光线暗淡,令人困惑。

回答

1

如果要动态更改UI的颜色,可以使用DynamicResource。例如,在App.xaml deckare SolidColorBrush

<Application.Resources> 
    <SolidColorBrush x:Key="DynamicColor" /> 
</Application.Resources> 

而在你的控制,你应该通过DynamicResource绑定:

<UserControl x:Class="" 
    <!--The code is omitted for the brevity-->   
    d:DesignHeight="600" d:DesignWidth="800"> 
    <Grid Name="mainGrid" Background="{DynamicResource DynamicColor}">  

    </Grid> 
</UserControl> 

然后改变Resource你应该:

Application.Current.Resources["YourResource"] = YourNewValue; 

让我秀价值如何变化的例子:

private void Window_ContentRendered(object sender, EventArgs e) 
{ 
    SolidColorBrush YourBrush = (Brush)(new BrushConverter().ConvertFrom("#455A65"));; 

    // Set the value 
    Application.Current.Resources["DynamicColor"] = YourBrush;   
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    SolidColorBrush YourBrush = Brushes.Orange; 

    // Set the value 
    Application.Current.Resources["DynamicColor"] = YourBrush; 
} 

DynamicResources用于更改。在哪里改变 - 这是开发者的愿望。

+0

Modernui有一个主题板,用户可以切换到ligth /黑暗的背景不能被硬编码“#455A65”它绑定到一些动态资源 – Zwan

+0

@Zwan请看我更新的答案。随意问任何问题。 – StepUp

+0

看看mainwindows代码什么都不叫动态,它有背景设置。 +我的用户控件已经有了selectedtheme的字体大小/颜色,它只是一些背景,默认情况下透明的添加视图。 – Zwan

0

我挖通过FirstFloor示例应用程序,发现有一些DynamicResource键用于主题格式化。我可以用它来获取我的主题的窗口背景色:

<SolidColorBrush x:Key="BackgroundBrush" Color="{DynamicResource WindowBackgroundColor}" /> 

然后用它的地方,就像...

<DataGrid Background="{StaticResource BackgroundBrush}" /> 

还有其他DynamicResource键,也包括

{DynamicResource WindowText} // for WindowForegroundColor 
{DynamicResource WindowBorder} // for WindowBorderBrush 
{DynamicResource WindowBackgroundColor} // for WindowBorderBackgroundBrush 
{DynamicResource DefaultFontFamily} // 
{DynamicResource DefaultFontSize} // 

我发现这些浏览MUI-master源代码。 欲了解更多,请打开mui-master.sln并导航到项目FirstFloor.ModernUI(类库)/Themes/ModernWindow.xaml和/Themes/ModernDialog.xaml

相关问题