繁体   English   中英

WPF:如何访问后面的ResourceDictionary代码中的元素

[英]WPF: How to access element in ResourceDictionary code behind

在我的应用程序中,我有一个控件,可以在时间轴视图(如日历)上呈现许多块。 通过为时间轴提供适当的DataTemplate,可以为这些块提供模板。

我想将块DataTemplate与主时间轴视图分开,将块放入其自己的XAML中。 这样,我为该块创建了XAML(称为Block.xaml),并将DataTemplate封装在此XAML的ResourceDictionary中。

我已经在XAML后面添加了代码(称为Block.xaml.cs),在其中我需要访问该块中的某些元素。 问题是ResourceDictionaries似乎隐藏了代码背后的元素,使我无法访问它们。 我不能改用UserControl-这似乎不起作用。

如何从后面的代码访问Block DataTemplate的元素?

在此先感谢您的帮助。


Block.xaml:

<ResourceDictionary x:Class="Project.Windows.MainInterface.TimelinePanel.Block" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Project.Windows.MainInterface.TimelinePanel" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:converters="clr-namespace:Project.Converters" >
<DataTemplate x:Key="ItemBlockTemplate">
    <Grid Name="BlockParent" Width="Auto" Height="Auto" MinHeight="50" ClipToBounds="True" SizeChanged="BlockParent_OnSizeChanged">
        <Border Panel.ZIndex="3" BorderBrush="{DynamicResource BackgroundGreyLight}" BorderThickness="1" CornerRadius="1" />
        <Grid Margin="1" Background="{DynamicResource BlockBackgroundGradient}" d:LayoutOverrides="Width">
            <TextBlock x:Name="blockName" Height="20" Margin="4,0,4,0" Padding="3" VerticalAlignment="Top" Panel.ZIndex="3" FontSize="10" Foreground="{DynamicResource TextLight}" Text="{Binding blockName}" TextTrimming="CharacterEllipsis" Visibility="Visible" />
            <TextBlock x:Name="Duration" Margin="0,2,4,2" Padding="0,0,3,0" HorizontalAlignment="Right" VerticalAlignment="Bottom" Panel.ZIndex="5" FontSize="10" Foreground="{DynamicResource TextLight}" Text="{Binding FormattedDuration}" ToolTip="{Binding Duration}" />
            <Grid Background="#FF0FA8FF" Opacity="0.7" />
        </Grid>
    </Grid>
</DataTemplate>

主界面中时间轴的代码段:

 ...
 <timeLineTool:TimeLineControl x:Name="Timeline" Height="50" MinWidth="50" Margin="0,0,12,0" HorizontalAlignment="Left" VerticalAlignment="Top" Items="{Binding Timeline}" SnapsToDevicePixels="True" UnitSize="{Binding UnitSize}" UseLayoutRounding="True">
        <timeLineTool:TimeLineControl.ItemTemplate>
            <DataTemplate DataType="{x:Type local:BlockItemViewModel}">
                <ContentControl>
                    <ContentPresenter Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Content="{Binding}" ContentTemplate="{StaticResource ItemBlockTemplate}" />
                </ContentControl>
            </DataTemplate>
        </timeLineTool:TimeLineControl.ItemTemplate>
    </timeLineTool:TimeLineControl>
 ...

...如果我可以在块中使用UserControl而不是ResourceDictionary,则可以解决此问题,因为所有元素都可以在后面的代码中自动公开供用户控件使用。

示例XAML:

 <Window.Resources>
      <ControlTemplate x:Key="ResourceName" TargetType="{x:Type Label}">
          <Label  Foreground="White" Content="{iconPacks:PackIconFontAwesome plug,Height=40,Width=40}"/>
      </ControlTemplate>
 </Window.Resources>

后面的代码

ControlTemplate字典:

 private Dictionary<string, ControlTemplate> collection
    {
        get
        {
            Dictionary<string, ControlTemplate> controlTemplates = new Dictionary<string, ControlTemplate>();
            controlTemplates.Add("ResourceName", FindResource("ResourceName") as ControlTemplate);
            return controlTemplates;
        }
    }

使用ControlTemplate:

    Label LBDisConnect = new Label();
    LBDisConnect.Template = collection["ResourceName"];
    LoginInfo.Children.Add(LBDisConnect);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM