كيف يمكنك تعطيل آثار تمرير الماوس على زر في WPF?

StackOverflow https://stackoverflow.com/questions/1224439

  •  11-07-2019
  •  | 
  •  

سؤال

أنا أحاول أن تعطيل تأثير تمرير الماوس على الأزرار, أو على الأقل تغيير اللون ، WPF.

أنا باستخدام النمط التالي:

<Style x:Key="Borderless" TargetType="{x:Type Button}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Button Background="{TemplateBinding Control.Background}"
                                Focusable="False">
                            <ContentPresenter
                  Margin="{TemplateBinding Control.Padding}"
                  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                  VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                  SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                  ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                  RecognizesAccessKey="True"
                  Content="{TemplateBinding ContentControl.Content}" />
                            </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

في النافذة.الموارد التي اعتقدت أن تجاوز كل الافتراضي السلوكيات.لكنه لا.

أي اقتراحات ؟

هل كانت مفيدة؟

المحلول

انظر ماذا التحكم الخاصة بك قالب يتلخص في:

<ControlTemplate TargetType="{x:Type Button}">
   <Button>
      <ContentPresenter/>
   </Button>
</ControlTemplate>

أنت تقول "أريد أن محل البحث من زر مع... زر." استخدام ControlTemplate هو استبدال البصرية شجرة التحكم.إذا كنت تقوم باستبدال شجرة البصرية القائمة الزر مع زر آخر.إذا كنت تريد أن تبدأ زر من الصفر ، حاول استخدام SimpleStyles زر:

<Style TargetType="{x:Type Button}">
   <Setter Property="SnapsToDevicePixels" Value="true"/>
   <Setter Property="OverridesDefaultStyle" Value="true"/>
   <Setter Property="MinHeight" Value="23"/>
   <Setter Property="MinWidth" Value="75"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
            <Border Name="Border" CornerRadius="2" BorderThickness="1"
                    Background="#C0C0C0"
                    BorderBrush="#404040">
               <ContentPresenter Margin="2" 
                                 HorizontalAlignment="Center"
                                 VerticalAlignment="Center" 
                                 RecognizesAccessKey="True"/>
            </Border>
            <ControlTemplate.Triggers>
               <Trigger Property="IsKeyboardFocused" Value="true">
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#202020" />
               </Trigger>
               <Trigger Property="IsDefaulted" Value="true">
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#202020" />
               </Trigger>
               <Trigger Property="IsMouseOver" Value="true">
                  <Setter TargetName="Border" 
                          Property="Background" Value="#808080" />
               </Trigger>
               <Trigger Property="IsPressed" Value="true">
                  <Setter TargetName="Border" 
                          Property="Background" Value="#E0E0E0" />
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#606060" />
               </Trigger>
               <Trigger Property="IsEnabled" Value="false">
                  <Setter TargetName="Border" 
                          Property="Background" Value="#EEEEEE" />
                  <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#AAAAAA" />
                  <Setter Property="Foreground" Value="#888888"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

لاحظ أن هذا القالب بإنشاء زر أبسط طريقة ممكنة:الحدود التي تحتوي على زر المحتوى.أنها لا تستخدم زر آخر جزءا لا يتجزأ من داخل القالب.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top