WPF:如何访问

时间:2017-08-23 10:53:54

标签: c# wpf xaml user-controls resourcedictionary

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

我想将块DataTemplate与主时间轴视图分开,将块放入自己的XAML中。因此,我为Block创建了一个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>
 ...

...如果我可以为我的Block使用UserControl而不是ResourceDictionary,这将解决问题,因为所有元素都会自动公开在用户控件的代码中。

1 个答案:

答案 0 :(得分:0)

示例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);