Search code examples
c#wpfxaml

The resource "x" could not be resolved


Main Window Xaml and I have problem with this line but idk why the resurce exist but the code cannot found

<Grid Width="300" HorizontalAlignment="Left">
                    <TextBlock Margin="20 0" VerticalAlignment="Center" Foreground="#b0b9c6" IsHitTestVisible="False"
                               Panel.ZIndex="1" Text="Ricerca..."
                               Visibility="{Binding ElementName=txtRicerca , Path=Text.IsEmpty,Converter={StaticResource BoolToVis}}"/>

                    <TextBox x:Name="txtSearch" Style="{StaticResource textboxSearch }"/>

                    <Icon:PackIconMaterial Kind="Magnify" Width="15" Height="15" VerticalAlignment="Center"
                                           Margin="0 0 15 0" Foreground="#b0b9c6" HorizontalAlignment="Right"/>

                </Grid>

App.Xaml

 <Style x:Name="textboxSearch" TargetType="TextBox">
            <Setter Property="Background" Value="#ffffff"/>
            <Setter Property="Foreground" Value="#b0b9c6" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Padding" Value="15 10" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Margin" Value="0 10" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBoxBase">
                        <Border x:Name="border" CornerRadius="20" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"
                                BorderThickness="1" BorderBrush="#e0e6ed">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden"
                                          VerticalScrollBarVisibility="Hidden"/>
                        </Border>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" Value="#d9d9d9" TargetName="border"/>
                            </Trigger>

                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter Property="BorderBrush" Value="#d9d9d9" TargetName="border"/>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

I tryed to chenge property, change name, delete, assign to athor textbox but nothig works


Solution

  • Change x:Name to x:Key in your App.xaml file:

    <Application ...>
        <Application.Resources>
            <Style x:Key="textboxSearch" TargetType="TextBox">
            ...
        </Application.Resources>
    </Application>