none
Usando dictionary con Style s ... No reconoce el Style="{Static Resource txt}" RRS feed

  • Pregunta

  • buenas tardes,

    Estoy intentando usar un Dictionary de Styles en WPF C# ... declaro un Style en el archivo

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:TelList">
        <Style x:Key="txt" TargetType="{x:Type TextBlock}">
            <Setter Property="FontFamily" Value="Microsoft Sans Serif"/>
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
        </Style> 
        
    </ResourceDictionary>

    lo declaro en App.xaml

    <ResourceDictionary x:Key="dic1" Source="..\\Dictionary1.xaml"/>

    y en esta línea me marca una Exception (en un Window)

    <TextBlock Grid.Column="7" Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource txt}"> 

    Muchas gracias por su ayuda

    Pablo


    miércoles, 26 de junio de 2019 20:09

Respuestas

Todas las respuestas

  • Deleted
    miércoles, 26 de junio de 2019 21:57
  • hola Walter,

    Gracias por responder

    Este es mi App.xaml. Si le agrego la linea del Dictionary abajo de Application.Resources ocurre un error...

    Por que será?

    <Application x:Class="TelList.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:sw="clr-namespace:TelList"  
                 StartupUri="MainWindow.xaml">
    
        <Application.Resources>
    
            
            
            <!--the brushes used in the template (use these to customize the window look along with the window style (see the bottom of this file)-->
            <SolidColorBrush x:Key="WindowColor" Color="Red"/>
            <SolidColorBrush x:Key="WindowBackColor" Color="Orange"/>
            <SolidColorBrush x:Key="WindowForeColor" Color="WhiteSmoke"/>
            <SolidColorBrush x:Key="WindowForeHighlightColor" Color="WhiteSmoke"/>
            <SolidColorBrush x:Key="WindowButtonColor" Color="Blue"/>
            <SolidColorBrush x:Key="WindowButtonOverColor" Color="LightBlue"/>
            <SolidColorBrush x:Key="WindowButtonPressedColor" Color="DarkBlue"/>
    
            <!---red window brushes-->
            <!--<SolidColorBrush x:Key="WindowColor" Color="DarkRed"/>
            <SolidColorBrush x:Key="WindowBackColor" Color="DarkRed"/>
            <SolidColorBrush x:Key="WindowForeColor" Color="White"/>
            <SolidColorBrush x:Key="WindowForeHighlightColor" Color="White"/>
            <SolidColorBrush x:Key="WindowButtonColor" Color="OrangeRed"/>
            <SolidColorBrush x:Key="WindowButtonOverColor" Color="Orange"/>
            <SolidColorBrush x:Key="WindowButtonPressedColor" Color="DarkOrange"/>-->
    
            <!--The window template and it's components -->
    
            <sw:WindowMinimizeCommand x:Key="MinimizeCommand"></sw:WindowMinimizeCommand>
            <sw:WindowMaximizeCommand x:Key="MaximizeCommand"></sw:WindowMaximizeCommand>
            <sw:WindowCloseCommand x:Key="CloseCommand"></sw:WindowCloseCommand>
    
            <Style TargetType="{x:Type Button}" x:Key="WindowButton">
                <Setter Property="Height" Value="25"/>
                <Setter Property="Width" Value="25"/>
                <Setter Property="Background" Value="{StaticResource WindowButtonColor}"/>
                <Setter Property="Foreground" Value="{StaticResource WindowForeColor}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Background" Value="{StaticResource WindowButtonColor}"/>
                                    <Setter Property="Foreground" Value="{StaticResource WindowColor}"/>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="{StaticResource WindowButtonOverColor}"/>
                                    <Setter Property="Foreground" Value="{StaticResource WindowForeHighlightColor}"/>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Background" Value="{StaticResource WindowButtonPressedColor}"/>
                                    <Setter Property="Foreground" Value="{StaticResource WindowForeColor}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
            <Style TargetType="{x:Type Image}" x:Key="WindowIcon">
                <Setter Property="Height" Value="25"/>
                <Setter Property="Width" Value="25"/>
            </Style>
    
            <ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
                <Grid Background="{StaticResource WindowBackColor}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" Margin="5,0,5,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="75"/>
                            </Grid.ColumnDefinitions>
                            <Label Grid.Column="0"                                           
                                        sw:ControlDoubleClickBehavior.ExecuteCommand="{StaticResource CloseCommand}"                                               
                                        sw:ControlDoubleClickBehavior.ExecuteCommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Padding="0">
                                <Image DockPanel.Dock="Left" Style="{StaticResource WindowIcon}" Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}},Path=Icon}"                                         
                                        Width="16"                                           
                                        sw:ShowSystemMenuBehavior.TargetWindow="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"                                           
                                        sw:ShowSystemMenuBehavior.LeftButtonShowAt="{Binding ElementName=WindowContent}"/>
                            </Label>
                            <Label Grid.Column="1" DockPanel.Dock="Left" Content="{TemplateBinding Property=Title}" Padding="3,0,0,0"                                                                                  
                                        HorizontalContentAlignment="Left"
                                        VerticalContentAlignment="Center"
                                        HorizontalAlignment="Stretch"
                                        VerticalAlignment="Stretch"
                                        Foreground="White"                                           
                                        FontSize="16"
                                        sw:WindowDragBehavior.LeftMouseButtonDrag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
                                        sw:ControlDoubleClickBehavior.ExecuteCommand="{StaticResource MaximizeCommand}"
                                        sw:ControlDoubleClickBehavior.ExecuteCommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
                                        sw:ShowSystemMenuBehavior.TargetWindow="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
                                        sw:ShowSystemMenuBehavior.RightButtonShow="true">
                            </Label>
                            <StackPanel Grid.Column="2" Orientation="Horizontal">
                                <Button DockPanel.Dock="Right" Style="{StaticResource WindowButton}" Command="{StaticResource MinimizeCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">_</Button>
                                <Button DockPanel.Dock="Right" Style="{StaticResource WindowButton}" Command="{StaticResource MaximizeCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">+</Button>
                                <Button DockPanel.Dock="Right"  Style="{StaticResource WindowButton}" Command="{StaticResource CloseCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">X</Button>
                            </StackPanel>
                        </Grid>
                        <Border Grid.Row="1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                            <Grid>
                                <AdornerDecorator>
                                    <ContentPresenter x:Name="WindowContent"/>
                                </AdornerDecorator>
                                <ResizeGrip  x:Name="WindowResizeGrip" HorizontalAlignment="Right" IsTabStop="false" Visibility="Collapsed" VerticalAlignment="Bottom"/>
                            </Grid>
                        </Border>
                    </Grid>
                    <Thumb x:Name="ThumbTop" Height="6" Margin="14,0" VerticalAlignment="Top" Cursor="SizeNS"  Opacity="0" sw:WindowResizeBehavior.TopResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                    <Thumb x:Name="ThumbTopLeft" Height="6" Width="6" VerticalAlignment="Top" HorizontalAlignment="Left" Cursor="SizeNWSE"  Opacity="0" sw:WindowResizeBehavior.TopLeftResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                    <Thumb x:Name="ThumbBottom" Height="6" Margin="14,0" VerticalAlignment="Bottom" Cursor="SizeNS" Opacity="0" sw:WindowResizeBehavior.BottomResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                    <Thumb x:Name="ThumbTopRight" Height="6" Width="6" VerticalAlignment="Top" HorizontalAlignment="Right" Cursor="SizeNESW"  Opacity="0" sw:WindowResizeBehavior.TopRightResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                    <Thumb x:Name="ThumbRight" HorizontalAlignment="Right" Margin="0,14" Width="6" Cursor="SizeWE"  Opacity="0" sw:WindowResizeBehavior.RightResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                    <Thumb x:Name="ThumbBottomRight" Height="6" Width="6" VerticalAlignment="Bottom" HorizontalAlignment="Right" Cursor="SizeNWSE"  Opacity="0" sw:WindowResizeBehavior.BottomRightResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                    <Thumb x:Name="ThumbLeft" HorizontalAlignment="Left" Margin="0,14" Width="6" Cursor="SizeWE"  Opacity="0" sw:WindowResizeBehavior.LeftResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                    <Thumb x:Name="ThumbBottomLeft" Height="6" Width="6" VerticalAlignment="Bottom" HorizontalAlignment="Left" Cursor="SizeNESW"  Opacity="0" sw:WindowResizeBehavior.BottomLeftResize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
                            <Condition Property="WindowState" Value="Normal"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
    
            <!-- the custom window style and the window template 
            Here you can customize the look of the window without having to touch the control template 
            or comment out the brushes and create a new style (see the red style below)-->
    
            <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
                <Setter Property="WindowStyle" Value="None"/>
                <Setter Property="AllowsTransparency" Value="True"/>
                <Setter Property="MinWidth" Value="200"/>
                <Setter Property="MinHeight" Value="46"/>
                <!--CaptionHeight + ResizeBorderThickness * 2-->
                <Setter Property="Background" Value="LightGreen"/>
                <Setter Property="BorderBrush" Value="LightGray"/>
                <Setter Property="BorderThickness" Value="7"/>
                <Setter Property="Foreground" Value="LightSkyBlue"/>
                <Setter Property="Template" Value="{StaticResource WindowTemplate}"/>
            </Style>
    
            <!--the red style window-->
            <Style x:Key="RedWindowStyle" TargetType="{x:Type Window}">
                <Setter Property="WindowStyle" Value="None"/>
                <Setter Property="AllowsTransparency" Value="True"/>
                <Setter Property="MinWidth" Value="100"/>
                <Setter Property="MinHeight" Value="46"/>
                <Setter Property="Background" Value="white"/>
                <Setter Property="BorderBrush" Value="DarkRed"/>
                <Setter Property="BorderThickness" Value="7"/>
                <Setter Property="Foreground" Value="DarkGray"/>
                <Setter Property="Template" Value="{StaticResource WindowTemplate}"/>
            </Style>
    
            <Style x:Key="RoundCorner" TargetType="{x:Type Button}">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Padding" Value="1"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Grid x:Name="grid">
                                <Border x:Name="border" CornerRadius="8" BorderBrush="Black" BorderThickness="2">
                                    <Border.Background>
                                        <RadialGradientBrush GradientOrigin="0.496,1.052">
                                            <RadialGradientBrush.RelativeTransform>
                                                <TransformGroup>
                                                    <ScaleTransform CenterX="0.5" CenterY="0.5" 
                                                            ScaleX="1.5" ScaleY="1.5"/>
                                                    <TranslateTransform X="0.02" Y="0.3"/>
                                                </TransformGroup>
                                            </RadialGradientBrush.RelativeTransform>
                                            <GradientStop Offset="1" Color="#00000000"/>
                                            <GradientStop Offset="0.3" Color="#FFFFFFFF"/>
                                        </RadialGradientBrush>
                                    </Border.Background>
                                    <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              TextElement.FontWeight="Bold">
                                    </ContentPresenter>
                                </Border>
    
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Background" TargetName="border">
                                        <Setter.Value>
                                            <RadialGradientBrush GradientOrigin="0.496,1.052">
                                                <RadialGradientBrush.RelativeTransform>
                                                    <TransformGroup>
                                                        <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/>
                                                        <TranslateTransform X="0.02" Y="0.3"/>
                                                    </TransformGroup>
                                                </RadialGradientBrush.RelativeTransform>
                                                <GradientStop Color="#00000000" Offset="1"/>
                                                <GradientStop Color="#FF303030" Offset="0.3"/>
                                            </RadialGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="BorderBrush" TargetName="border" Value="#FF33962B"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Opacity" TargetName="grid" Value="0.25"/>
                                </Trigger>
    
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Application.Resources>
    </Application>
    

    jueves, 27 de junio de 2019 2:27
  • Deleted
    • Marcado como respuesta Tigre Pablito jueves, 27 de junio de 2019 23:01
    jueves, 27 de junio de 2019 5:10