Domanda

My style test is defined as a resource in a ResourceDictionary in UserControl, like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="test" TargetType="ContentControl">
    <!--<Setter Property="Visibility" Value="Visible" />-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <TextBlock Text="TESTTTT" Foreground="Black"/>
            </ControlTemplate>
          </Setter.Value>
       </Setter>
   </Style>
</ResourceDictionary>

UserControl:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Dictionary1.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

In the code behind file of this UserControl I'am trying to get that Style and apply it to a content control, which is defined in the same UserControl.

Here is my UserControl.xaml:

<UserControl x:Class="WpfApplication2.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="350" Width="525" Loaded="Window_Loaded">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <ContentControl Name="testControl" />
    </Grid>
</UserControl>

and in code-behind in the Loaded event handler I wrote:

UserControl m = sender as UserControl;
Style s = m.Resources["test"] as Style; 
// I also tried to reference style in App resources, but it didn't make any difference 
//(Style)Application.Current.FindResource("test");
m.testControl = new ContentControl();
m.testControl.Style = s;
m.testControl.ApplyTemplate();

In debugging mode I saw the found style. Template Controls can also be found by searching for the using their keys/Names. But they wont be shown. It just show an empty user control without any controls from my template defined in the style.

I hope you can help me and thanks in advance!

È stato utile?

Soluzione

In this case, you create new ContentControl, but do not add to the current VisualTree, respectively, it is not visible.

Also, there is no property testControl in a UserControl, because . symbol used for access to the properties of Class, therefore remove m before testControl or use this instead:

UserControl m = sender as UserControl;
Style s = m.Resources["test"] as Style; 
m.testControl = new ContentControl(); // Remove this line
m.testControl.Style = s;              // and 'm', or write like this: this.testControl.Style = s;
m.testControl.ApplyTemplate();

And the final result is:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    var control = sender as UserControl;

    if (control != null)
    {
        Style s = control.Resources["test"] as Style;
        testControl.Style = s;

        // control.ApplyTemplate(); // it's not necessary in your case
    }            
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top