2008-12-16 7 views
6

मेरे पास एक बटन है जो यह सुनिश्चित करने के लिए 2 comboboxes को देख रहा है कि यह सक्षम होने से पहले उनके पास मूल्य है। समस्या यह है कि मैं जिस तरह से कर रहा हूं वह मेरी थीम प्रोजेक्ट में घोषित डिफ़ॉल्ट शैली को ओवरराइट कर रहा है।WPF बटन IsEnabled कॉम्बोबॉक्स चयन के आधार पर डिफ़ॉल्ट शैली ओवरराइटिंग

<Button x:Name="btnOK" VerticalAlignment="Center" Content="OK" IsDefault="True" Margin="0" Click="btnOK_Click"> 
        <Button.Style> 
         <Style BasedOn="{StaticResource DefaultButton}"> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding ElementName=ddlWageTypes, Path=SelectedItem}" Value="{x:Null}"> 
            <Setter Property="Button.IsEnabled" Value="false"/> 
           </DataTrigger> 
           <DataTrigger Binding="{Binding ElementName=ddlJobTitles, Path=SelectedItem}" Value="{x:Null}"> 
            <Setter Property="Button.IsEnabled" Value="false"/> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Button.Style> 
       </Button> 

मैं शैली टैग करने के लिए basedon = "{StaticResouce MyDefaultButtonStyleName}" जोड़ने की कोशिश की है, लेकिन यह क्रम पर चल रही है।

त्रुटि "System.Windows.Style 'मान ऑब्जेक्ट' System.Windows.Controls.Button 'की संपत्ति' शैली 'को असाइन नहीं किया जा सकता है। केवल लक्ष्य प्रकार वाले स्टाइल पर आधारित हो सकता है जो मूल प्रकार है' IFrameworkInputElement '। मार्कअप फ़ाइल में ऑब्जेक्ट' System.Windows.Style 'पर त्रुटि "

क्या डिफ़ॉल्ट शैली को ओवरराइट किए बिना XAML में ऐसा करना था।

संपादित करें: कोड नमूना अपडेट किया गया। मुझे OKButtonStyle पर एक त्रुटि मिलती है, "संपत्ति 'संसाधन' में तत्व नहीं जोड़ सकता है, क्योंकि संपत्ति में केवल एक बाल तत्व हो सकता है यदि यह एक स्पष्ट संग्रह टैग का उपयोग करता है। मार्कअप फ़ाइल में ऑब्जेक्ट 'System.Windows.Style' पर त्रुटि"

<UserControl x:Class="UK.Budgeting.XBAP.ShiftDiff.NewFTEPremiumPaySummary" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:compModel="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
    xmlns:local="clr-namespace:UK.Budgeting.XBAP.ShiftDiff"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="CellTemplates.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 

     <Style TargetType="{x:Type Button}" x:Key="OKButtonStyle" BasedOn="{StaticResource DefaultButton}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding ElementName=ddlWageTypes, Path=SelectedItem}" Value="{x:Null}"> 
      <Setter Property="Button.IsEnabled" Value="false"/> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding ElementName=ddlJobTitles, Path=SelectedItem}" Value="{x:Null}"> 
      <Setter Property="Button.IsEnabled" Value="false"/> 
      </DataTrigger> 
     </Style.Triggers> 
     </Style> 

    </UserControl.Resources> 
    <Grid> 
     <Rectangle Style="{StaticResource DialogRectangle}"/> 
     <Border Style="{StaticResource DialogBorder}"> 
      <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Background="White"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition/> 
        <ColumnDefinition Width="5"/> 
        <ColumnDefinition MinWidth="300"/> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition/> 
        <RowDefinition Height="2"/> 
        <RowDefinition/> 
        <RowDefinition Height="2"/> 
        <RowDefinition/> 
        <RowDefinition Height="2"/> 
        <RowDefinition/> 
       </Grid.RowDefinitions> 

       <TextBlock Grid.Column="0" Grid.Row="0" Style="{StaticResource LabelStyle}">Wage Type</TextBlock> 
       <TextBlock Grid.Column="0" Grid.Row="2" Style="{StaticResource LabelStyle}">Job Title</TextBlock> 

       <ComboBox x:Name="ddlWageTypes" VerticalAlignment="Top" Grid.Column="2" Grid.Row="0" 
          DisplayMemberPath="DisplayName" 
          SelectedValuePath="WageTypeCode"/> 
       <ComboBox x:Name="ddlJobTitles" VerticalAlignment="Top" Grid.Column="2" Grid.Row="2" 
          DisplayMemberPath="JobTitle" 
          SelectedValuePath="JobCode"/> 

       <StackPanel Grid.Column="2" Grid.Row="6" VerticalAlignment="Top" Orientation="Horizontal" Margin="5"> 
        <Button x:Name="btnOK" VerticalAlignment="Center" Content="OK" IsDefault="True" Margin="0" Click="btnOK_Click" Style="{StaticResource OKButtonStyle}"/> 
        <Button x:Name="btnCancel" VerticalAlignment="Center" Content="Cancel" IsCancel="True" Margin="10,0,0,0" Click="btnCancel_Click"/> 
       </StackPanel> 
      </Grid> 
     </Border> 
    </Grid> 
</UserControl> 
+0

आप भी हमें OKButtonStyle के लिए परिभाषा दिखाने सकते हैं? –

+0

बटन के अंदर शैली। स्टाइल को एक लक्ष्य टाइप की आवश्यकता है। –

उत्तर

9

यह कैसे

BasedOn="{StaticResouce DefaultButton}" 

चाहिए डिफ़ॉल्ट बटन शैली का उल्लेख करने के लिए है? यह क्रैश हो जाता है क्योंकि डिफॉल्टबटन आपके ऐप में एक अपरिभाषित संसाधन है।

यह होना चाहिए:

BasedOn="{StaticResource {x:Type Button}}" 

संपादित करें: क्षमा करें, बहुत जल्दी से जवाब दे दिया।

मैंने देखा है कि अब आपके बटन में एक स्टाइल = {} सेट है, और ओकेबटन स्टाइल नामक शैली में इशारा कर रहा है। यह वह शैली है जो सब कुछ परिभाषित करनी चाहिए और डिफ़ॉल्ट बटन शैली पर आधारित होना चाहिए। सबकुछ से, मैं उन ट्रिगर्स को शामिल करता हूं। एक्सएएमएल में आप क्या कह रहे हैं कि स्टाइल आपके बटन की सामग्री है।

हो सकता है कि कुछ कोड में मदद मिलेगी:

<Window x:Class="WindowsApplication7.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="WindowsApplication7" Height="300" Width="300" 
    > 
    <Window.Resources> 
    <Style TargetType="{x:Type Button}" x:Key="defaultButtonStyle"> 
     <Setter Property="Background" Value="Red" /> 
    </Style> 

    <Style TargetType="{x:Type Button}" x:Key="okButtonStyle" BasedOn="{StaticResource defaultButtonStyle}"> 
     <Setter Property="Foreground" Value="Green" /> 
     <Style.Triggers> 
     <Trigger Property="IsEnabled" Value="True"> 
      <Setter Property="Background" Value="Yellow" /> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="False"> 
      <Setter Property="Foreground" Value="Blue" /> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </Window.Resources> 
    <StackPanel> 
    <Button>System default</Button> 
    <Button Style="{StaticResource defaultButtonStyle}">My Default</Button> 
    <Button Style="{StaticResource okButtonStyle}">Ok</Button> 
    <Button Style="{StaticResource okButtonStyle}" IsEnabled="False">Ok disabled</Button> 
    </StackPanel> 
</Window> 
+0

basedon = "{StaticResouce DefaultButton}" एक नामित शैली उदाहरण एक्स पर आधारित है: प्रकार बटन पूर्व: <शैली एक्स: मुख्य = "DefaultButton" TargetType = "{x: प्रकार बटन}"> .... – Nate

+0

उस siz के बारे में खेद है, OKButtonStyle एक और रास्ता था जिसे मैं इसे समझने की कोशिश में नीचे चला गया। मैंने स्टाइल को बटन के भीतर से लिया, इसे मेरे संसाधनों में जोड़ा और फिर डिफॉल्टबटन पर बेसिंग करने की कोशिश की, लेकिन यह एक अलग त्रुटि के साथ उड़ा। – Nate

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