2013-06-18 13 views
6

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

कृपया मदद करें।

धन्यवाद

+1

आपको लगता है कि पीछे किसी भी EventTriggers या कोड है छवि बदल सकता है? –

+0

नहीं। बटन पर छवि कहीं से भी नहीं बदलेगी। मैं डिजाइन समय पर Xaml में छवि निर्दिष्ट कर रहा हूँ। लेकिन, अगर मुझे ट्रिगर्स पर छवि बदलनी है तो कोई समस्या नहीं है, मैं भी ऐसा करूंगा। – WAQ

+0

क्या आप अपना कोड पोस्ट कर सकते हैं? स्टाइल बटन और बटन निर्माण। – Sonhja

उत्तर

8

आपके साथ क्या हो रहा है सामान्य है। जब आप कोई बटन बनाते हैं, तो यदि आप उन्हें बदलते/ओवरराइड नहीं करते हैं तो यह इसकी डिफ़ॉल्ट गुणों का उपयोग करेगा।

इस मामले में, जब आप अपना बटन बनाते हैं, तो आप 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" /> 
+0

मैंने आपके द्वारा प्रदान किए गए कोड का उपयोग किया था। अगर मैं इसे नहीं देता तो महान काम करें x: key = "बटन स्टाइल"। सभी बटन स्टाइल व्यवहार प्राप्त करते हैं। लेकिन जब मैं अपना आवश्यक बटन स्टाइल = "बटन स्टाइल" देता हूं, तो मुझे "संपत्ति सेट करें" सिस्टम का कहना है कि एक अपवाद मिलता है। Windows.FrameworkElement.Style 'ने अपवाद फेंक दिया "। इस पर कोई विचार है? – WAQ

+1

@WasimQadir यह 'Style =" {StaticResource ButtonStyled} होना चाहिए "' – lisp

+0

@lisp बहुत धन्यवाद दोस्तों। यह काम किया :) – WAQ

2

मैं पार्टी के लिए देर से एक छोटे से कर रहा हूँ, लेकिन आप यह सब बहुत जटिल कर रहे हैं।

आपको बस इतना करना सामग्री और पृष्ठभूमि सेट कर दिया जाता है:

<Button x:Name="YouTubeButton" Width="148" Height="76" BorderBrush="Black" 
    Click="YouTubeButton_Click" Margin="112,20,112,0" 
    MouseEnter="Button_OnMouseEnter" MouseLeave="Button_MouseLeave"> 
    <Button.Content> 
     <Image Source="Images/YouTube.png" /> 
    </Button.Content> 
    <Button.Background> 
     <ImageBrush ImageSource="Images/YouTube.png" /> 
    </Button.Background> 
</Button> 

(Optional - makes the mouse behave like a hand clicking a link) 
#region Button_MouseLeave(object sender, MouseEventArgs e) 
/// <summary> 
/// This event is fired when the mouse leaves a button 
/// </summary> 
private void Button_MouseLeave(object sender, MouseEventArgs e) 
{ 
    // Change the cursor back to default 
    Cursor = null; 
} 
#endregion 

#region Button_OnMouseEnter(object sender, EventArgs e) 
/// <summary> 
/// This event is fired when the mouse hovers over a button 
/// </summary> 
public void Button_OnMouseEnter(object sender, EventArgs e) 
{ 
    // Change the cursor to a Hand pointer 
    Cursor = Cursors.Hand; 
} 
#endregion 
+0

मदद करने के लिए खुशी हुई यह बटन के अंदर एक और नियंत्रण जोड़ती है लेकिन यह बहुत बेहतर है! धन्यवाद, मैं इसका इस्तेमाल करूंगा! – Sree

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