WPF

2009-10-20 9 views
7

में सभी व्युत्पन्न कक्षाओं में एक शैली को लागू करना मैं नियंत्रण से प्राप्त सभी वर्गों में एक शैली लागू करना चाहता हूं। क्या यह WPF के साथ संभव है? निम्नलिखित उदाहरण काम नहीं करता है। मैं लेबल, टेक्स्ट बॉक्स और बटन चाहते हैं 4.WPF

<Window x:Class="WeatherInfo.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Wetterbericht" Height="300" Width="300"> 
    <Window.Resources> 
     <Style TargetType="Control"> 
      <Setter Property="Margin" Value="4"/> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <StackPanel Margin="4" HorizontalAlignment="Left">    
      <Label>Zipcode</Label> 
      <TextBox Name="Zipcode"></TextBox> 
      <Button>get weather info</Button> 
     </StackPanel> 
    </Grid> 
</Window> 

उत्तर

10

यहाँ एक समाधान है: एक आकर्षण की तरह

<Window.Resources> 
    <Style TargetType="Control" x:Key="BaseStyle"> 
     <Setter Property="Margin" Value="4"/> 
    </Style> 
    <Style BasedOn="{StaticResource BaseStyle}" TargetType="Button" /> 
    <Style BasedOn="{StaticResource BaseStyle}" TargetType="Label" /> 
    <Style BasedOn="{StaticResource BaseStyle}" TargetType="TextBox" /> 
</Window.Resources> 
<Grid> 
    <StackPanel Margin="4" HorizontalAlignment="Left"> 
     <Label>Zipcode</Label> 
     <TextBox Name="Zipcode"></TextBox> 
     <Button>get weather info</Button> 
    </StackPanel> 
</Grid> 
+0

काम करता है ... धन्यवाद। – Sebastian

6

एक मार्जिन इस WPF में संभव नहीं है के लिए। आपकी मदद करने के लिए आपके पास कुछ विकल्प हैं:

  1. आधारितऑन विशेषता का उपयोग कर एक शैली के आधार पर एक शैली बनाएं।
  2. सामान्य जानकारी (मार्जिन, इस मामले में) को एक संसाधन और संदर्भ में ले जाएं जो आपके द्वारा बनाई गई प्रत्येक शैली से संसाधन है।

उदाहरण के 1

<Style TargetType="Control"> 
    <Setter Property="Margin" Value="4"/> 
</Style> 

<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type Control}}"> 
</Style> 

उदाहरण के 2

<Thickness x:Key="MarginSize">4</Thickness> 

<Style TargetType="TextBox"> 
    <Setter Property="Margin" Value="{StaticResource MarginSize}"/> 
</Style> 
संबंधित मुद्दे

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