mans
mans

Reputation: 18168

how to use an application resource in user control xaml

I have a app.xml with some resources in it such as:

<Application.Resources>
    <con:EnumToVisibilityConverter x:Key="EnumToVisibilityConverter" />
    <con:NegateBoolConverter x:Key="NegateBoolConverter" />
    <Style TargetType="FrameworkElement" x:Key="BaseStyle">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="Margin" Value="3"/>
        <Setter Property="Width" Value="120"/>
        <Setter Property="Height" Value="25" />
    </Style>
    <Style TargetType="CheckBox" BasedOn="{StaticResource BaseStyle}">
    </Style>
    <Style TargetType="DatePicker" BasedOn="{StaticResource BaseStyle}">
    </Style>
    <Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">

    </Style>
    <Style TargetType="Label"  BasedOn="{StaticResource BaseStyle}">

    </Style>

    <Style TargetType="Button" BasedOn="{StaticResource BaseStyle}">
        <Setter Property="Width" Value="75" />
        <Setter Property="Height" Value="23" />
        <Setter Property="Margin" Value="5,2,2,5" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Top" />
    </Style>

</Application.Resources>

I want to use them in my user control xaml as follow:

 <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="App.xaml" />
        </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
 </UserControl.Resources>

Since my user control xaml is in View directory, the above mentione syntax is not accepted by system and complained that it can not find view/app.xaml. How can I add path to the Source so it can find it?

Upvotes: 1

Views: 3040

Answers (1)

Carlo
Carlo

Reputation: 25959

You don't need to do that. Application resources are available all over the app, so all you need to do is either this in xaml:

Style="{StaticResource BaseStyle}"

Or this in C# (inside your control's code):

Style baseStyle = (Style)this.FindResource("BaseStyle");

Upvotes: 2

Related Questions