आपके साथ क्या हो रहा है सामान्य है। जब आप कोई बटन बनाते हैं, तो यदि आप उन्हें बदलते/ओवरराइड नहीं करते हैं तो यह इसकी डिफ़ॉल्ट गुणों का उपयोग करेगा।
इस मामले में, जब आप अपना बटन बनाते हैं, तो आप Background
संपत्ति ओवरराइड कर रहे हैं, लेकिन केवल आपके बटन की सामान्य स्थिति के लिए। यदि आप होवरिंग करते समय भी पृष्ठभूमि बदलना चाहते हैं, तो आपको ऐसा करने के लिए बटन बताना चाहिए।
इस प्रयोजन के लिए, मैं सुझाव है कि आप बटन खाका ओवरराइड करने के लिए शैलियों का उपयोग कर, इस तरह:
<Window x:Class="ButtonTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ImageBrush x:Key="ButtonImage" ImageSource="/ButtonTest;component/Resources/ok.png"></ImageBrush>
<Style TargetType="Button">
<Setter Property="Background" Value="{StaticResource ButtonImage}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="Cursor" Value="Hand" />
<!-- If we don't tell the background to change on hover, it will remain the same -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="84,75,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
इस मामले में, इस शैली अपने सभी बटन के लिए लागू किया जाएगा। आप निर्दिष्ट कर सकते हैं जो बटन अपने बटन में अपनी शैली के लिए एक x:Key
जोड़कर शैली लागू करने के लिए और फिर निर्धारित करें यह:
<Style TargetType="Button" x:Key="ButtonStyled">
.....
<Button Style="{StaticResource ButtonStyled}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="84,75,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
आपको लगता है कि पीछे किसी भी EventTriggers या कोड है छवि बदल सकता है? –
नहीं। बटन पर छवि कहीं से भी नहीं बदलेगी। मैं डिजाइन समय पर Xaml में छवि निर्दिष्ट कर रहा हूँ। लेकिन, अगर मुझे ट्रिगर्स पर छवि बदलनी है तो कोई समस्या नहीं है, मैं भी ऐसा करूंगा। – WAQ
क्या आप अपना कोड पोस्ट कर सकते हैं? स्टाइल बटन और बटन निर्माण। – Sonhja