Search code examples
wpfxamltriggerscontroltemplateresourcedictionary

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?


Solution

  • 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);
                }
            }