使用样式在应用程序资源中定义

时间:2012-03-05 17:32:56

标签: wpf

我在使用其他类中的Application资源中定义的任何样式时遇到问题。这些是我的App.xaml类的内容。当我尝试应用TextBox类时,我收到错误:找不到命名资源“validationTextboxStyle”

<Application x:Class="ClientApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
         StartupUri="LoginWindow.xaml">
<Application.Resources>
    <Style x:Key="validationTextboxStyle" TargetType="TextBox">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <TextBlock DockPanel.Dock="Right" 
                    Foreground="Orange"
                    FontSize="12pt">
                    !!!!
                        </TextBlock>
                        <Border BorderBrush="Green" BorderThickness="1">
                            <AdornedElementPlaceholder />
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Application.Resources>
<Application.MainWindow>
    <NavigationWindow Source="Main.xaml"></NavigationWindow>
</Application.MainWindow>

我尝试在另一个窗口中使用该样式。

<Window x:Class="ClientApp.NewItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:validation="clr-namespace:ClientApp.ValidationRules"
    xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
    Height="520" Width="400"
    WindowStartupLocation="CenterOwner">
<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Save"
                    CanExecute="SaveCanExecute"
                    Executed="SaveExecuted" />
</Window.CommandBindings>
<Window.Resources>

</Window.Resources>

<TabControl Height="Auto" Name="tabControl1" Width="Auto">
    <TabItem Header="General" Name="tabItem1">
        <extToolkit:BusyIndicator x:Name="busyIndicator" Visibility="Visible">
            <Grid Name="contentGrid">


                <extToolkit:ChildWindow Name="generateWindowChild" IsModal="True" Height="222" Width="300" WindowStartupLocation="Center" Grid.Column="0" Grid.ColumnSpan="6" Grid.Row="0" Grid.RowSpan="10">
                    <Grid>
                        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,14,0,0" Name="txt_password" VerticalAlignment="Top" Width="242" />
                        <CheckBox Content="Numbers" IsChecked="True" Height="16" HorizontalAlignment="Left" Margin="16,79,0,0" Name="chk_numbers" VerticalAlignment="Top" />
                        <Button Content="Generate" Height="23" HorizontalAlignment="Left" Margin="58,152,0,0" Name="btn_generatePass" VerticalAlignment="Top" Width="75" Click="btn_generatePass_Click" />
                        <CheckBox Content="Upper case letters" IsChecked="True" Height="16" HorizontalAlignment="Left" Margin="16,123,0,0" Name="chk_special" VerticalAlignment="Top" />
                        <CheckBox Content="Special characters" IsChecked="True" Height="16" HorizontalAlignment="Left" Margin="16,101,0,0" Name="chk_upper" VerticalAlignment="Top" />
                        <Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="145,152,0,0" Name="btn_InsertPass" VerticalAlignment="Top" Width="75" Click="btn_InsertPass_Click" />
                        <extToolkit:IntegerUpDown DefaultValue="8" Value="8" Height="25" Width="35" Visibility="Visible" Margin="88,46,145,115" Name="minUpDown" PreviewKeyDown="minUpDown_PreviewKeyDown" ValueChanged="minUpDown_ValueChanged" />
                        <extToolkit:IntegerUpDown DefaultValue="8" Value="8" Height="25" Width="35" Margin="0,46,24,115" Name="maxUpDown" PreviewKeyDown="maxUpDown_PreviewKeyDown" ValueChanged="maxUpDown_ValueChanged" HorizontalAlignment="Right" />
                        <Label Content="Min length:" Height="28" HorizontalAlignment="Left" Margin="16,45,0,0" Name="minLengthLabel" VerticalAlignment="Top" />
                        <Label Content="Max length:" Height="28" HorizontalAlignment="Right" Margin="0,45,69,0" Name="maxLengthLabel" VerticalAlignment="Top" />
                    </Grid>
                </extToolkit:ChildWindow>

                <TextBox HorizontalAlignment="Left" Style="{StaticResource validationTextboxStyle}" VerticalAlignment="Top" Width="120" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" LostFocus="TextBox_LostFocus">
                    <TextBox.Text>
                        <Binding Path="GroupName" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
                            <Binding.ValidationRules><validation:ItemGroupNameValidationRule /></Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <TextBox HorizontalAlignment="Left" Style="{StaticResource validationTextboxStyle}" VerticalAlignment="Top" Width="120" Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="2" >
                    <TextBox.Text>
                        <Binding Path="Title" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
                            <Binding.ValidationRules><validation:ItemTitleValidationRule /></Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="2">
                    <TextBox.Text>
                        <Binding Path="username" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
                            <Binding.ValidationRules><validation:ItemUsernameValidationRule /></Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

                <PasswordBox HorizontalAlignment="Left" Name="txt_itemPassword" VerticalAlignment="Top" Width="120" Grid.Row="5" Grid.Column="2" Grid.ColumnSpan="2" LostFocus="txt_itemPassword_LostFocus" />
                <PasswordBox HorizontalAlignment="Left" Name="txt_ConfirmPassword" VerticalAlignment="Top" Width="120" Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" LostKeyboardFocus="txt_ConfirmPassword_LostKeyboardFocus" />
                <Button Content="Generate" HorizontalAlignment="Left" Name="btn_generateNewPass" VerticalAlignment="Top" Width="36" Click="btn_generateNewPass_Click" Grid.Row="6" Grid.Column="4" Grid.ColumnSpan="2"/>
                <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Grid.Row="7" Grid.Column="2" Grid.ColumnSpan="2" >
                    <TextBox.Text>
                        <Binding Path="Url" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
                            <Binding.ValidationRules><validation:ItemUrlValidationRule /></Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

                <TextBox Height="80" Width="200" Background="White" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="8" Grid.Column="2" Grid.ColumnSpan="6">
                    <TextBox.Text>
                        <Binding Path="Note" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
                            <Binding.ValidationRules><validation:ItemNoteValidationRule /></Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

                <ListBox Height="80" HorizontalAlignment="Left" BorderBrush="LightGray" Name="listBox_Roles" VerticalAlignment="Top" Width="200" Grid.Row="9" Grid.Column="2" Grid.ColumnSpan="4">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Title}" Checked="checkBoxChanged" Unchecked="checkBoxChanged"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

                <Label Content="Group" HorizontalAlignment="Left" Name="label9" VerticalAlignment="Top" Grid.Row="2" Grid.Column="1" />
                <Label Content="Title" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Grid.Row="3" Grid.Column="1"/>
                <Label Content="Username" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" Grid.Row="4" Grid.Column="1"/>
                <Label Content="Password" HorizontalAlignment="Left" Name="label3" VerticalAlignment="Top" Grid.Row="5" Grid.Column="1"/>
                <Label Content="Confirm Password" HorizontalAlignment="Left" Name="label7" VerticalAlignment="Top" Grid.Row="6" Grid.Column="1"/>
                <Label Content="Url" HorizontalAlignment="Left" Name="label5" VerticalAlignment="Top" Grid.Row="7" Grid.Column="1"/>
                <Label Content="Note" HorizontalAlignment="Left" Name="label4" VerticalAlignment="Top" Grid.Row="8" Grid.Column="1"/>
                <Label Content="Roles" HorizontalAlignment="Left" Name="label8" VerticalAlignment="Top" Grid.Row="9" Grid.Column="1"/>

                <Button Content="Create" Command="ApplicationCommands.Save" IsDefault="True" HorizontalAlignment="Left" Name="btn_CreateItem" VerticalAlignment="Top" Width="75" Grid.Row="11" Grid.Column="2"/>
                <Button Content="Cancel" HorizontalAlignment="Right" IsCancel="True" Name="btn_CancelCreateItem" VerticalAlignment="Top" Width="75" Click="btn_CancelCreateItem_Click" Grid.Row="11" Grid.Column="3" Grid.ColumnSpan="2"/>
            </Grid>
        </extToolkit:BusyIndicator>
    </TabItem>
    <TabItem Header="Expiration" Name="tabItem2">
        <GroupBox Header="Expiration Rule" Height="229" Name="groupBox1" Width="469" VerticalAlignment="top" HorizontalAlignment="Left">
            <Grid Height="210">
                <Grid.RowDefinitions>
                    <RowDefinition Height="66*" />
                    <RowDefinition Height="144*" />
                </Grid.RowDefinitions>
                <RadioButton GroupName="passwordExpiration" Content="Never" IsChecked="True" Height="16" HorizontalAlignment="Left" Margin="38,22,0,0" Name="radioExpiresNever" VerticalAlignment="Top" />
                <RadioButton GroupName="passwordExpiration" Content="In" Height="16" Name="radioExpiresInDays" Margin="36,65,-36,129" Grid.RowSpan="2" />              
                <extToolkit:IntegerUpDown Name="expiresInDaysUpDown" Margin="152,61,232,127" Height="22" Grid.RowSpan="2"></extToolkit:IntegerUpDown>
                <Label Name="daysRecurring" Content="Days" Margin="231,58,0,117" HorizontalAlignment="Left" Width="38" Grid.RowSpan="2"></Label>
                <RadioButton GroupName="passwordExpiration" Content="On" Height="16" HorizontalAlignment="Left" Margin="38,38,0,0" Name="radioExpiresDate" VerticalAlignment="Top" Grid.Row="1" />
                <extToolkit:DateTimePicker Name="expirationDate" Width="203" Height="21" Margin="152,40,102,83" Grid.Row="1"></extToolkit:DateTimePicker>
            </Grid>
        </GroupBox>
    </TabItem>
</TabControl>

1 个答案:

答案 0 :(得分:0)

如果App类中有构造函数,请确保同时添加InitializeComponent(); 否则,它将不会加载App.xaml中定义的资源。