2016-03-04 38 views
2

下面的代碼創建了當你把鼠標移動到物品得到橙色listboxitems一個窗口:顏色在Windows Server中的WPF應用程序錯誤2012

<Window x:Class="Orange.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > 

    <Window.Resources> 
     <Style TargetType="ListBoxItem"> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="Orange" /> 
       </Trigger> 
       </Style.Triggers> 
      </Style> 
    </Window.Resources> 

    <ListBox> 
     <ListBoxItem> aaaa </ListBoxItem> 
     <ListBoxItem> aaaa </ListBoxItem> 
     <ListBoxItem> aaaa </ListBoxItem> 
    </ListBox> 
</Window> 

如果你執行它在任何操作系統但Windows 2012它工作正常。在Windows 2012 Server上,背景顯示爲白色。

enter image description here

任何想法?

+0

好吧,這很奇怪。如果你嘗試像F90那樣的顏色的直接十六進制值? –

回答

2

我使用的是Windows 10(1511版),它不適用於我的系統。

我用VS 2015的文檔大綱來創建一個ListBoxItem模板。我修改了它的橙色背景,它在Win10上工作(並且,我假設Server 2012)。

<Window x:Class="WpfApplication20.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > 
    <Window.Resources> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
         <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"> 
          <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Border> 
         <ControlTemplate.Triggers> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsMouseOver" Value="True"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="Orange"/> 
           <Setter Property="BorderBrush" TargetName="Bd" Value="#A826A0DA"/> 
          </MultiTrigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="Selector.IsSelectionActive" Value="False"/> 
            <Condition Property="IsSelected" Value="True"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="#3DDADADA"/> 
           <Setter Property="BorderBrush" TargetName="Bd" Value="#FFDADADA"/> 
          </MultiTrigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="Selector.IsSelectionActive" Value="True"/> 
            <Condition Property="IsSelected" Value="True"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="#3D26A0DA"/> 
           <Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/> 
          </MultiTrigger> 
          <Trigger Property="IsEnabled" Value="False"> 
           <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <ListBox> 
     <ListBoxItem>aaaa</ListBoxItem> 
     <ListBoxItem>aaaa</ListBoxItem> 
     <ListBoxItem>aaaa</ListBoxItem> 
    </ListBox> 
</Window> 

看看IsMouseOver是如何觸發的,它將邊界背景設置爲硬編碼值?這就是爲什麼你的風格不起作用......背景可能正在改變,但邊界不使用它!使用Snoop,我發現ListBoxItem的背景確實變成了橙色,但正如我所說的,背景使用了模板觸發器中的顏色。

您可能需要做的是創建一個樣式,然後將其他樣式基於它。我在我的Win7虛擬機上使用VS 2013來創建一個基本模板,該模板應該接近您現有的樣式所期望的。在App.xaml中把這個:

<Style TargetType="ListBoxItem"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"> 
        <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" Value="True"/> 
          <Condition Property="Selector.IsSelectionActive" Value="False"/> 
         </MultiTrigger.Conditions> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

,然後改變你的風格是基於關閉它,就像這樣:

<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="Orange" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

希望它可以幫助...可能不理想,但它應該工作。

+0

很高興知道。我還沒有嘗試過使用Windows 10.無論如何,我希望我能找到一個不涉及更改代碼的解決方案。我們有很多自定義樣式,因爲應用程序旨在提供豐富的用戶體驗。 –

+0

嗯,聽起來像是一個bug。你認爲我應該在M $填寫一個嗎? –

+0

你可以但我認爲這個問題從Windows 8開始,所以我很確定他們知道。你在Windows 8上試過了嗎? Server 2012基於Win 8,所以我認爲這是行不通的。 –

相關問題