2011-09-29 9 views
5

डेटा प्रविष्टि फ़ॉर्म को स्टाइल करने के लिए नीचे कुछ लिखना असामान्य नहीं है, लेकिन मेरी समस्या यह है कि TextBox और TextBlockBaseElementStyle में स्थित सेटर्स को लागू करने के लिए प्रतीत नहीं होता है। आम तौर पर मुझे उन्हें अलग से परिभाषित करने की आवश्यकता होती है।मेरा टेक्स्टब्लॉक/टेक्स्टबॉक्स क्यों बेस स्टाइल से मूल्य लागू नहीं करता है?

यह क्यों है? और क्या इसके चारों ओर एक रास्ता है?

मैं इसे इस तथ्य वे आम तौर पर अन्य नियंत्रण टेम्पलेट्स में उपयोग किया जाता (उदाहरण के लिए TextBlock सबसे नियंत्रण में प्रयोग किया जाता है और पाठबॉक्स Datepickers में प्रयोग किया जाता है और comboboxes)

<Style x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}"> 
    <Setter Property="Margin" Value="5" /> 
    <Setter Property="VerticalAlignment" Value="Center" /> 
</Style> 
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" /> 

उत्तर

8

मैं चाहते हैं के साथ क्या करना है अनुमान लगा रहा हूँ दो संभावित कामकाज का सुझाव देने के लिए। ऐसा लगता है कि प्रत्येक कुंजी और प्रकार का उपयोग किया जा सकता है लेकिन उनमें से दोनों को आपके प्रश्न केस, x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}" के रूप में उपयोग नहीं किया जा सकता है।

  1. का उपयोग कर एक्स: मुख्य

    <Style x:Key="BaseElementStyle"> 
        <Setter Property="FrameworkElement.Margin" Value="5" /> 
        <Setter Property="FrameworkElement.VerticalAlignment" Value="Center" /> 
    </Style> 
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
    
  2. का उपयोग कर एक्स: प्रकार

    <Style TargetType="{x:Type FrameworkElement}"> 
        <Setter Property="Margin" Value="5" /> 
        <Setter Property="VerticalAlignment" Value="Center" /> 
    </Style> 
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    
+0

धन्यवाद! और आपने मुझे कुछ नया सिखाया ... मुझे पता नहीं था कि आप एक अंतर्निहित शैली पर एक शैली का आधार बना सकते हैं जैसे कि आप # 2 में दिखाते हैं। जानना बहुत अच्छा है! – Rachel

7

यह भी ध्यान है कि WPF समझता है एक ControlTemplate में रखने के एक मुद्रास्फीति सीमा हो सकता है और करता है करने के लिए टेम्पलेट्स के अंदर डिफ़ॉल्ट शैलियों को लागू नहीं करते हैं। नियम के अपवाद: Controlसे प्राप्त कुछ भी डिफ़ॉल्ट शैली के साथ बढ़े होगा। चूंकि TextBlockFrameworkElement से विरासत में है और नियंत्रण से नहीं, यदि आप ControlTemplate के अंदर इसका उपयोग करते हैं तो आपको इसकी शैली को मैन्युअल रूप से भी लागू करना होगा। यह टेक्स्टब्लॉक दोनों के लिए सच है जो हाथ से जोड़े गए हैं, या स्ट्रिंग Content स्ट्रिंग के लिए WPF द्वारा जोड़े गए टेक्स्टब्लॉक द्वारा। एक त्वरित उदाहरण:

<Window x:Class="ImplicitStyles.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"> 
    <StackPanel> 
     <StackPanel.Resources> 
      <Style x:Key="BaseElementStyle"> 
       <Setter Property="FrameworkElement.Tag" Value="Hello World" /> 
      </Style> 
      <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseElementStyle}" /> 
      <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" /> 

      <!-- Default style for TextBlock will not be applied, Tag will be null --> 
      <ControlTemplate x:Key="MyContentControlTemplateOne" TargetType="{x:Type ContentControl}"> 
       <Border BorderBrush="Red" BorderThickness="2"> 
        <TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" /> 
       </Border> 
      </ControlTemplate> 

      <!-- Default style for Button will be applied, Tag will be Hello World -->  
      <ControlTemplate x:Key="MyContentControlTemplateTwo" TargetType="{x:Type ContentControl}"> 
       <Border BorderBrush="Red" BorderThickness="2"> 
        <Button Content="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" /> 
       </Border> 
      </ControlTemplate> 

     </StackPanel.Resources> 

     <ContentControl Template="{StaticResource MyContentControlTemplateOne}" /> 
     <ContentControl Template="{StaticResource MyContentControlTemplateTwo}" /> 
    </StackPanel> 

</Window> 

अधिक जानकारी के लिए इस ब्लॉग पोस्ट देखें:

http://blogs.msdn.com/b/wpfsdk/archive/2009/08/27/implicit-styles-templates-controls-and-frameworkelements.aspx

+1

धन्यवाद, मुझे नहीं पता था कि टेम्पलेट शैली विरासत के लिए सीमाएं थीं। मैं सोच रहा था कि क्यों DataTemplates के अंदर TextBlocks एक ही शैली लागू नहीं हो रही थी – Rachel

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