【问题标题】:Include XAML Trigger To ControlTemplate将 XAML 触发器包含到 ControlTemplate
【发布时间】:2012-10-22 10:59:33
【问题描述】:

我对 WPF 很陌生,正在做一些自定义控件的工作... 我的问题是一个文件中的代码会很多,所以我想要 将代码拆分为单独的文件,这样查看该代码的其他人就不会不知所措。

好的,我的问题... 我有一个 ResourceDictionary...“Generic.xaml” 在这个文件中得到了一个 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>

现在我想将上面代码的“ControlTemplate.Triggers”部分放在另一个 .XAML 文件中。

这可能吗?

【问题讨论】:

    标签: wpf xaml triggers controltemplate resourcedictionary


    【解决方案1】:

    又是我。对不起,我花了这么长时间才回答。 我现在领先了几步...我在另一个 ResourceDictionary 中获得了触发器,如果​​我在实现控件的“Window.Resources”中实现它,一切正常。

    我现在的问题是......我不想在“Window.Resources”中实现 ResourceDictionary,而是在自定义控件的“ControlTemplate.Resources”中实现。但是当我这样做时,它会告诉我:

    “无法将“Microsoft.Expression.Markup.DocumentModel.DocumentCompositeNode”类型的对象转换为“System.Windows.ResourceDictionary”类型。

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

    如果我将 Customer.xaml ResourceDictionary 中的代码复制到 ControlTemplate.Resources 中,它可以工作...但我希望它在一个附加文件中...

    有什么想法吗?

    【讨论】:

    • 我遇到了同样的问题 - 似乎 Visual Studio仍然不喜欢控件模板中的资源字典。
    【解决方案2】:

    查看Resource Dictionaries。这应该允许您跨文件拆分样式。

    【讨论】:

      【解决方案3】:

      用一个讨厌的解决方法做到了...我将与您分享。

      我刚刚实现了一个在加载 DataGrid 时触发的事件。 在这种情况下,我加载了我想添加到控件模板的 ResourceDictionarys 在 ResourceDictionary 对象中。然后我遍历 ResourceDictionary 对象中的所有条目,并将每个单独的条目添加到 ControlTemplate 的资源中...... 代码如下:

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

      【讨论】:

        猜你喜欢
        • 2018-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-31
        • 1970-01-01
        • 2011-08-22
        相关资源
        最近更新 更多