Robert Nagel
Robert Nagel

Reputation: 149

Include XAML Trigger To ControlTemplate

I am pretty new with WPF and am doing some custom control stuff... My problem is that the code in one file is going to be way to much, so i want to split the code in seperate files, so other people looking in that code aren´t going to be overwhelmed.

Okay to my question... I got a ResourceDictionary... the "Generic.xaml" In this file a got the template of an DataGrid:

    <Style TargetType="{x:Type local:BADataGrid}">
    <Style.Setters>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:BADataGrid">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" 
                        Padding="{TemplateBinding Padding}" 
                        SnapsToDevicePixels="True">

           <!-- *SOME TEMPLATE CODE* -->

                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="GridStyle" Value="CUSTOMER">
                            <Trigger.Setters>
                                <Setter Property="ColumnHeaderStyle">
                                    <Setter.Value>
                                        <Style TargetType="{x:Type DataGridColumnHeader}">
                                            <Setter Property="Background">
                                                <Setter.Value>
                                                    <ImageBrush>
                                                        <ImageBrush.ImageSource>
                                                            <Binding  Path="HeaderBackground" RelativeSource="{RelativeSource AncestorType=local:BADataGrid}">
                                                                <Binding.TargetNullValue>
                                                                    <ImageSource>
                                                                        headerBack.png
                                                                    </ImageSource>
                                                                </Binding.TargetNullValue>
                                                            </Binding>
                                                        </ImageBrush.ImageSource>
                                                    </ImageBrush>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Setter.Value>
                                </Setter>
                            </Trigger.Setters>
                        </Trigger>
                    </ControlTemplate.Triggers>

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

Now I want the "ControlTemplate.Triggers" part of the code above in another .XAML File.

Is this even possible?

Upvotes: 2

Views: 1275

Answers (3)

Robert Nagel
Robert Nagel

Reputation: 149

Did it with a nasty workaround... I´ll just share it with you.

I just implemented an event that is fired when the DataGrid is loaded. In this event I load the ResourceDictionarys i wanted to add to the control template in ResourceDictionary object´s. Then i iterate through all entrys in the ResourceDictionary object and add each seperate to the ControlTemplate´s resources... Here´s the code:

void DataGridLoaded(object sender, RoutedEventArgs e)
    {
        BADataGrid dg = (BADataGrid)VisualTreeHelper.GetParent((DependencyObject)sender);


        List<string> resourceList = new List<string>();
        resourceList.Add(Properties.Resources.Customer);

        foreach (string s in resourceList)
        {
            System.Xml.XmlReader xmlReader = new System.Xml.XmlTextReader(new System.IO.StringReader(s));
            ResourceDictionary resource = (ResourceDictionary)XamlReader.Load(xmlReader);

            foreach (System.Collections.DictionaryEntry item in resource)
            {
                dg.Resources.Add(item.Key, item.Value);
            }
        }

Upvotes: 0

Robert Nagel
Robert Nagel

Reputation: 149

it´s me again. Sorry it took me so long to answer. I am a few steps ahead now... I got the Trigger in an additional ResourceDictionary, and if I impletment it in the "Window.Resources" where I implement the control everything works just fine.

My problem now is... I don´t want to implement the ResourceDictionary in the "Window.Resources" but in the "ControlTemplate.Resources" of my custom control. But when I do so it tell´s me :

"Unable to cast object of type 'Microsoft.Expression.Markup.DocumentModel.DocumentCompositeNode' to type 'System.Windows.ResourceDictionary'.

                    <ControlTemplate.Resources>
                        <ResourceDictionary>
                            <ResourceDictionary.MergedDictionaries>
                                <ResourceDictionary
                                Source="Customer.xaml" />
                            </ResourceDictionary.MergedDictionaries>
                        </ResourceDictionary>
                    </ControlTemplate.Resources>

If I copy the code inside the Customer.xaml ResourceDictionary into the ControlTemplate.Resources it works... but I want it in an additional file...

Any ideas?

Upvotes: 1

Sharath
Sharath

Reputation: 969

Look into Resource Dictionaries. That should allow you to split styles across files.

Upvotes: 0

Related Questions