在我的应用程序中,我有一个控件,它在时间线视图(如日历)上呈现多个块。人们可以通过给时间线一个适当的DataTemplate来为块提供一个模板。
我想将块DataTemplate从主时间线视图中分离出来,将块放入自己的XAML中。因此,我为块(称为Block.xaml)创建了一个XAML,并将DataTemplate封装在一个ResourceDictionary中,在这个XAML中。
我在XAML (称为Block.xaml.cs)后面添加了一段代码,在该代码中我需要访问块中的一些元素。问题是,ResourceDictionaries似乎将元素隐藏在代码背后,以至于我无法访问它们。我不能用UserControl代替-这似乎不起作用。
如何从后面的代码访问块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>
 ......If --我可以用一个UserControl而不是一个ResourceDictionary来解决这个问题,因为在用户控件后面的代码中,所有元素都是公开可用的。
发布于 2017-08-23 11:01:36
样本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);https://stackoverflow.com/questions/45837695
复制相似问题