2016-03-04 21 views
5

मैं WPF के लिए बिल्कुल नया हूं लेकिन .NET (Winforms) में अनुभव किया है। मैं चयनित आइटम के केंद्रित और फ़ोकस किए गए रंग को नियंत्रित करने के लिए सूचीबॉक्स की हाइलाइट शैली में हेरफेर करने की कोशिश कर रहा हूं। इस पर मैंने पाया है कि प्रत्येक एकल ट्यूटोरियल सिस्टमकॉलर्स के लिए एक नया मान असाइन करने के लिए एक कस्टम शैली का उपयोग करता है। हाइलाइटब्रशकी और सिस्टमकॉलर्स। कंट्रोलब्रशकी। लेकिन यह काम नहीं कर रहा है। काम करने के लिए अनगिनत घंटों के बाद, यह मेरे लिए हुआ कि शायद यह ओएस से संबंधित था। मैं इसे विंडोज 10 सिस्टम पर कोशिश कर रहा था। मैंने विंडोज 7 सेटअप पर एक ही कोड चलाया, और देखो और देखो, यह काम किया!विंडोज़ में डब्ल्यूपीएफ लिस्टबॉक्स हाइलाइट 10

तो स्पष्ट रूप से पुरानी विधि विंडोज 10 में काम नहीं करती है (कम से कम यह मेरे जैसा दिखता है)। क्या किसी को कोई विकल्प मिला है? दिन के अंत में, मैं सिर्फ सूची बॉक्स को चमकदार हाइलाइट बनाए रखना चाहता हूं, भले ही इसमें फोकस न हो। डिफ़ॉल्ट ग्रे हाइलाइट देखना मुश्किल है, और कुछ उपयोगों में उचित प्रतीत नहीं होता है। मेरे पास असली दुनिया परिदृश्य है जहां फोकसबॉक्स से फोकस दूर होने पर मूल रूप से गायब होने के लिए हाइलाइट के लिए यह बहुत ही अप्राकृतिक लगता है।

नीचे एक्सएएमएल कोड है जो मैंने विंडोज 7 पर काम किया लेकिन विंडोज 10 पर नहीं किया। (वैसे, मैंने SystemColors.ControlBrushKey को SystemColors.InactiveSelectionHighlightBrushKey के साथ बदलने की भी कोशिश की है - परिणाम समान थे)।

<Window x:Class="TestApp.TestWindow" 
     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:local="clr-namespace:TestApp" 
     mc:Ignorable="d" 
     Title="TestWindow" Height="300" Width="300" Loaded="Window_Loaded"> 
    <Window.Resources> 
     <Style x:Key="myListboxStyle"> 
      <Style.Resources> 
       <!-- Background of selected item when focused --> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" /> 
       <!-- Background of selected item when not focused --> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" /> 
      </Style.Resources> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <ListBox x:Name="listBox" Style="{StaticResource myListboxStyle}" HorizontalAlignment="Left" Height="100" Margin="22,18,0,0" VerticalAlignment="Top" Width="237"> 
      <ListBoxItem>Test 1</ListBoxItem> 
      <ListBoxItem>Test 2</ListBoxItem> 
      <ListBoxItem>Test 3</ListBoxItem> 
     </ListBox> 
      <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="50,165,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> 

    </Grid> 
</Window> 

उत्तर

5

यदि आप वास्तव में अपने ऐप के लिए डिफ़ॉल्ट बदलना चाहते हैं, तो आप हमेशा टेम्पलेट की प्रति बनाकर शैली को संशोधित कर सकते हैं। इस मामले में ListBoxItem शैली।

डिज़ाइनर में, ListBoxItem पर क्लिक करें, टेम्पलेट संपादित करें पर क्लिक करें, और एक प्रतिलिपि संपादित करें।

नीचे मेरी मशीन पर जो कुछ मिला है, और लाल/हरे रंग के साथ चल रहे ऐप का एक स्क्रीनशॉट प्रभावित वस्तुओं पर लागू शैली का प्रदर्शन करता है। आप निश्चित रूप से सभी आइटम पर यह लागू होगा ...

<Window.Resources> 
    <Style x:Key="FocusVisual"> 
     <Setter Property="Control.Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/> 
    <SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/> 
    <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/> 
    <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/> 
    <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/> 
    <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/> 
    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="SnapsToDevicePixels" Value="True"/> 
     <Setter Property="Padding" Value="4,1"/> 
     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
     <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 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="{StaticResource Item.MouseOver.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/> 
         </MultiTrigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="False"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/> 
         </MultiTrigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="True"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/> 
         </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> 
    <Style x:Key="ListBoxItemStyle2" TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="SnapsToDevicePixels" Value="True"/> 
     <Setter Property="Padding" Value="4,1"/> 
     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
     <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 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="{StaticResource Item.MouseOver.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/> 
         </MultiTrigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="False"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="Red"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/> 
         </MultiTrigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="True"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="Green"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/> 
         </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> 

<Grid x:Name="LayoutRoot" Opacity="{Binding MainWindowOpacity}"> 
    <StackPanel> 
     <TextBlock Text="WPF" FontSize="36" Margin="20" Foreground="Orange" HorizontalAlignment="Center"/> 
     <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="237"> 
      <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">Test 1</ListBoxItem> 
      <ListBoxItem Style="{DynamicResource ListBoxItemStyle2}">Test 2</ListBoxItem> 
      <ListBoxItem>Test 3</ListBoxItem> 
     </ListBox> 
    </StackPanel> 
</Grid> 

और कार्रवाई में एप्लिकेशन:

सक्रिय:

Active

निष्क्रिय:

Inactive

कैविट एम्प्टर।

+0

धन्यवाद कोरी। मैं पहले से ही आपके जैसे समाधान के साथ खेल रहा था (मैंने पहले ट्री व्यू की उपस्थिति को अनुकूलित करने के लिए ऐसा कुछ इस्तेमाल किया था), लेकिन आपके दिशानिर्देशों ने मुझे वांछित टुकड़े दिए जो वास्तव में चाहते थे। – Cosmicjive

+0

यह मेरे लिए काम करता है, निराशाजनक है कि यह काम करने के लिए विंडोज 10 में बहुत कुछ लेता है ... – 00jt

0

मुझे विंडोज 10 के तहत लिस्टबॉक्स की हाइलाइट शैली के साथ एक ही समस्या थी ... फिर से पुरानी सिस्टम कॉलर्स। हाइलाइटब्रशकी और सिस्टमकॉलर्स। नियंत्रण ब्रशकी समाधान केवल विंडोज 7 के तहत मेरे लिए काम करता था। मेरे मामले में, मैं बस हटा देना चाहता था किसी भी WPF- खींचे गए हाइलाइटिंग पर, इसलिए मैं इसे अपने आप से बदल सकता था।

इस के लिए, मैं बस ControlTemplate निम्नलिखित संक्षिप्त शैली के माध्यम से मेरी संसाधन में बदल दिया:

 <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

मैं आम तौर पर वर्बोज़ ControlTemplates जगह नापसंद करते हैं, लेकिन यह मेरे लिए एक अच्छा संक्षिप्त समाधान था, उम्मीद है यह किसी को मदद कर सकता है अन्य।

3

माइक्रोसॉफ्ट ने इसे विंडोज 10 के लिए तोड़ दिया, लेकिन हम इसे ठीक कर सकते हैं!

<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="#1F26A0DA"/> 
       <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> 

सूचना वे हार्ड-कोडेड रंग के लिए मूल्यों, "# 1F26A0DA" की तरह है:

यहाँ टेम्पलेट विंडोज़ 10 (बस भागों मैं के बारे में परवाह) में ऐसा दिखाई देता है।

विंडोज 7 में, ListBoxItems के लिए निर्मित टेम्पलेट था:

  <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
... 
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
         <ContentPresenter 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> 

तो मूल रूप से माइक्रोसॉफ्ट विंडोज़ 7 में "SystemColors.InactiveSelectionHighlightBrushKey" की तरह संसाधनों का उपयोग कर रहा था लेकिन अब वे ऐसा हम नहीं कर सकते बना टेम्पलेट को ओवरराइड किए बिना ऐसा करें; चूंकि वे सभी मूल्यों में कड़ी-कोडित हैं।

तो इसे पैच करने के लिए, बस App.Xaml फ़ाइल में ListBoxItem के लिए टेम्पलेट को ओवरराइड करें; ताकि सब कुछ पैच हो।

<Style TargetType="{x:Type ListBoxItem}"> 
     <Style.Resources> <!-- Use your own colors here if you want, or do it per class -->  
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFFFA500"/> 
      <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="#FFFFA500"/> 
     </Style.Resources> 

     <Setter Property="Template"> 
<!-- Revert the Template in Windows 10 to match the Windows 7 template that used "SystemColors.HighlightBrushKey" and such--> 
       <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 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> 
0

मुझे डर है कि मैं Windows 10 विशेष रूप से इस परीक्षण करने के लिए एक तरीका नहीं है कि हूँ, लेकिन मैं "ControlBrushKey" संसाधन में परिभाषित बस नहीं दिया सिर्फ विंडोज 7 पर एक ऐसी ही समस्या का सामना किया काम नहीं कर रहा हूँ

"InactiveSelectionHighlightBrushKey" की स्थापना मेरे लिए काम किया:

<ListBox.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue" /> 
</ListBox.Resources> 

डोजर नीले डिफ़ॉल्ट एयरो चयन करने के लिए एक बहुत करीबी मुकाबला नहीं है; यह सिर्फ एक बाल गहरा है। मैं अभी भी यह पता लगाने पर काम कर रहा हूं कि इसे एक विशिष्ट रंग के बजाय वास्तविक चयन मूल्य पर कैसे मैप करना है।

संबंधित मुद्दे