2016-09-26 45 views
1

我使用的是MahAppsMetro,因此我有一個ResourceDictionary,我可以在其中更改設計的顏色。 現在我將顏色改爲藍色。但我不知道如何在combobox/table中更改selected itemtextcolor。 現在看起來是這樣的:更改組合框/資源類型中鼠標懸停的項目textcolor

Example: Combobox

Example: Table

所以現在我想通過資源字典的文字顏色改爲白色.. 它看起來是這樣的尚未:

<!-- re-set brushes too --> 
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" options:Freeze="True" /> 
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" /> 
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}" options:Freeze="True" /> 
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}" options:Freeze="True" /> 
<SolidColorBrush x:Key="AccentColorBrush4" Color="{StaticResource AccentColor4}" options:Freeze="True" /> 

<SolidColorBrush x:Key="WindowTitleColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" /> 

<LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5" options:Freeze="True"> 
    <GradientStop Color="{StaticResource HighlightColor}" Offset="0" /> 
    <GradientStop Color="{StaticResource AccentColor3}" Offset="1" /> 
</LinearGradientBrush> 

<SolidColorBrush x:Key="CheckmarkFill" Color="{StaticResource AccentColor}" options:Freeze="True" /> 
<SolidColorBrush x:Key="RightArrowFill" Color="{StaticResource AccentColor}" options:Freeze="True" /> 

你能告訴我什麼我需要添加到我的ResourceDictionary在項目的MouseOver上有一個白色文本。

希望我很清楚自己想做什麼。否則請告訴我你需要什麼進一步的信息。

編輯:

對於Combobox是現在的工作。但不適用於DataGrid。 這裏是我加入DataGrid

<DataGrid x:Name="mydatagrid" SelectionUnit="FullRow" HorizontalAlignment="Left" Margin="159,33,0,0" VerticalAlignment="Top" Height="204" Width="275" Background="{x:Null}" BorderBrush="Transparent" CanUserAddRows="False" GridLinesVisibility="None" BorderThickness="0" CanUserResizeRows="False"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="User" Binding="{Binding User, Mode=OneWay}" Width="100" IsReadOnly="True" CanUserResize="False" /> 
         <DataGridTextColumn Header="Vote" Binding="{Binding Vote, Mode=OneWay}" Width="90" IsReadOnly="True" CanUserResize="False" /> 
         <DataGridTemplateColumn Header="Status" Width="66" IsReadOnly="True" CanUserResize="False"> 
          <DataGridTemplateColumn.CellTemplate> 
           <DataTemplate> 
            <Image Source="{Binding Status, Mode=OneWay}" Width="16"/> 
           </DataTemplate> 
          </DataGridTemplateColumn.CellTemplate> 
         </DataGridTemplateColumn> 
         <DataGridTextColumn Header="Real_Username" Binding="{Binding Real_Username, Mode=OneWay}" Width="90" IsReadOnly="True" CanUserResize="False"/> 
        </DataGrid.Columns> 
       </DataGrid> 

我嘗試不同的方法添加到我的Application.xaml和擴大IsMouseOver的觸發器的:

<Setter Property="Controls.Foreground" Value="{DynamicResource AccentSelectedColorBrush}" /> 

我在做什麼錯?

+0

我從來沒有嘗試過自己,但我會建議尋找觸發器見:http://www.wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/ – MikeT

回答

0

這是可能的,但對於MouseOver,您需要重寫MahApps ComboBoxItem並使用觸發器對其進行擴展。 SelectedItem使用ColorBrush:AccentSelectedColorBrush。 原來這裏是MetroComboBoxItem與觸發器延長了鼠標懸停

的App.xaml

<Application x:Class="WpfApplication.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     StartupUri="MainWindow.xaml"> 
<Application.Resources> 
<ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
    <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! --> 
    <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" /> 
    <!-- Accent and AppTheme setting --> 
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /> 
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 

    <SolidColorBrush x:Key="AccentSelectedColorBrush" Color="DeepPink" /> 
    <Style TargetType="ComboBoxItem" x:Key="MetroComboBoxItem"> 
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" /> 
    <Setter Property="Padding" Value="2" /> 
    <Setter Property="HorizontalContentAlignment" Value="Left" /> 
    <Setter Property="VerticalContentAlignment" Value="Top" /> 
    <Setter Property="Background" Value="{DynamicResource TransparentWhiteBrush}" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ComboBoxItem"> 
       <Grid Background="{TemplateBinding Background}" Margin="0,0.5"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal" /> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" 
                    Storyboard.TargetName="MouseOverRectangle"> 
             <EasingDoubleKeyFrame KeyTime="0:0:0.1" 
                   Value=".65" /> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" 
                    Storyboard.TargetName="contentPresenter"> 
             <SplineDoubleKeyFrame KeyTime="0" 
                   Value=".55" /> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="SelectionStates"> 
          <VisualState x:Name="Unselected" /> 
          <VisualState x:Name="Selected"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" 
                    Storyboard.TargetName="SelectedRectangle"> 
             <EasingDoubleKeyFrame KeyTime="0:0:0.1" 
                   Value="1" /> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard /> 
          </VisualState> 
          <VisualState x:Name="Unfocused" /> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Rectangle x:Name="SelectedRectangle" 
           IsHitTestVisible="False" 
           Opacity="0" 
           Fill="{DynamicResource AccentColorBrush}" /> 
        <Rectangle x:Name="MouseOverRectangle" 
           IsHitTestVisible="False" 
           Opacity="0" 
           Fill="{DynamicResource AccentColorBrush3}" /> 
        <ContentControl Foreground="{TemplateBinding Foreground}"> 
         <ContentPresenter x:Name="contentPresenter" 
              Margin="{TemplateBinding Padding}" 
              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" /> 
        </ContentControl> 
        <Rectangle x:Name="FocusVisualElement" 
           Stroke="{DynamicResource HighlightBrush}" 
           StrokeThickness="1" 
           Visibility="Collapsed" /> 
       </Grid> 

       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}" /> 
        </Trigger> 
        <!-- This Trigger is new --> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    </ResourceDictionary> 
    </Application.Resources> 
</Application> 
+0

感謝您的答案。我只是搜索了你提供的代碼的一部分,當我用ILSpy打開DLL的時候發現了它。 因此,我認爲我找到了正確的地方去做這些改變。 不幸的是,我不知道如何改變它..使用ILSpy,我只能閱讀它。你能或者有人向我解釋如何在.dll文件中更改此部分? 或者我錯了我想做什麼? –

+0

爲什麼不將代碼片段添加到您的Window.Resources?然後完成。更改dll是沒有必要的。第二種選擇是直接更改源代碼(從GhHub https://github.com/MahApps/MahApps.Metro下載MahApps的源代碼並更改Controls.ComboBox.xaml)(可以使用觸發器添加樣式)並編譯您的代碼自己的MahApps.dll – WPFGermany

+0

我有多個窗口,我使用幾個組合框。因此,我只想在所有組合框的一點上進行更改。 這就是爲什麼我更喜歡第二種選擇。我已經下載了源代碼,在Visual Studio中打開它,並找到了添加我的更改的正確位置 - 但它顯示錯誤「名稱」TextBoxHelper「不存在於命名空間」clr-namespace:MahApps.Metro.Controls「中。因此,我無法編譯我自己的DLL ..任何想法我做錯了什麼? –

0

對於DataGrid中很容易:

,如果你喜歡的風格全系細胞,OnMouseOverIsSelected使用本:

<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}"> 
    <Setter Property="Foreground" Value="Green" /> 
     <Style.Triggers> 
      <!-- Full Row --> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=IsMouseOver}" Value="True"> 
       <Setter Property="Foreground" Value="Red" /> 
       <Setter Property="Opacity" Value="0.8" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=IsSelected}" Value="True"> 
       <Setter Property="Foreground" Value="DeepPink" /> 
       <Setter Property="Opacity" Value="0.3" /> 
      </DataTrigger> 
     </Style.Triggers> 
</Style> 

細胞只有這個:

<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}"> 
    <Setter Property="Foreground" Value="Green" /> 
     <Style.Triggers> 
      <!-- Cell only --> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"> 
       <Setter Property="Foreground" Value="Red" /> 
        <Setter Property="Opacity" Value="0.8" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"> 
       <Setter Property="Foreground" Value="DeepPink" /> 
       <Setter Property="Opacity" Value="0.3" /> 
      </DataTrigger> 
     </Style.Triggers> 
</Style> 

您可以將Style放在您的app.xaml中,以便爲您的整個應用程序工作。

+0

我認爲我想讓它變得複雜。您的解決方案對我來說工作得很好。謝謝!但是我還有一個問題 - 是否可以減少這一行的不透明度?所以它比選擇的那個要輕一點? 我只是嘗試使用其他答案中的代碼片段,其中不透明度降低但不起作用... –

+0

查看更新後的答案。不要忘記提出答案! – WPFGermany

+0

嗯 - 是否也可以改變背景的不透明度?現在它改變了整行(Fore-和Backrgound) 另一個問題:我有不同的Style.xaml文件,我設置不同的顏色。在運行時,我可以更改它們並將它們替換爲application.xaml,但是當我這樣做時,Foregroundcolor的值將不會更新 - 它總是保持原樣加載首先加載的xaml文件中。 –