是否可以在代码中访问DataTemplate中的项目?

发布于 2024-10-02 07:15:01 字数 820 浏览 3 评论 0原文

是否可以在代码中访问DataTemplate中的项目?我的意思是,我在 Window.Resources 中定义了 DataTemplate,如下所示:

<Window.Resources>
    <DataTemplate x:Key="MyTemplate" DataType="MyType">
        <StackPanel Orientation="Horizontal" Height="Auto" Tag="ColorString">
            <Rectangle Width="16" Height="14" Margin="2,1" VerticalAlignment="Center" Stroke="Red">
                <Rectangle.Fill>
                    <SolidColorBrush Color="{Binding Path=FillColor}"/>
                </Rectangle.Fill>
            </Rectangle>
            <TextBlock VerticalAlignment="Center" Text="{Binding Path=Value}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

我需要在代码中设置 Rectangle 的一些属性。我该怎么做?当 DataTemplate 加载时,其 VisualTree 为 null。

Is it possible to access items in DataTemplate in the code? I mean, I have DataTemplate defined in Window.Resources like this:

<Window.Resources>
    <DataTemplate x:Key="MyTemplate" DataType="MyType">
        <StackPanel Orientation="Horizontal" Height="Auto" Tag="ColorString">
            <Rectangle Width="16" Height="14" Margin="2,1" VerticalAlignment="Center" Stroke="Red">
                <Rectangle.Fill>
                    <SolidColorBrush Color="{Binding Path=FillColor}"/>
                </Rectangle.Fill>
            </Rectangle>
            <TextBlock VerticalAlignment="Center" Text="{Binding Path=Value}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

I need to set some properties of the Rectangle in the code. How can I do this? When DataTemplate is loaded its VisualTree is null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2024-10-09 07:15:01

您可以将 Loaded 事件添加到 StackPanel 和后面的代码中。

private void StackPanel_Loaded(object sender, RoutedEventArgs e)
{
    StackPanel stackPanel = sender as StackPanel;
    Rectangle rectangle = GetVisualChild<Rectangle>(stackPanel) as Rectangle;
    //..
}

这不会影响 DataTemplate 本身,但每次使用它时,都会调用 StackPanel_Loaded。

GetVisualChild 的实现..

public static T GetVisualChild<T>(object parent) where T : Visual
{
    DependencyObject dependencyObject = parent as DependencyObject;
    return InternalGetVisualChild<T>(dependencyObject);
}
private static T InternalGetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}

You can add the Loaded event to the StackPanel and in code behind.

private void StackPanel_Loaded(object sender, RoutedEventArgs e)
{
    StackPanel stackPanel = sender as StackPanel;
    Rectangle rectangle = GetVisualChild<Rectangle>(stackPanel) as Rectangle;
    //..
}

This will not affect the DataTemplate itself but everytime it is used, StackPanel_Loaded will be called.

An implementation of GetVisualChild..

public static T GetVisualChild<T>(object parent) where T : Visual
{
    DependencyObject dependencyObject = parent as DependencyObject;
    return InternalGetVisualChild<T>(dependencyObject);
}
private static T InternalGetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文