【问题标题】:How to get Children Objects of a DataTemplate through the use of ContentControl如何通过使用 ContentControl 获取 DataTemplate 的子对象
【发布时间】:2020-05-06 21:28:56
【问题描述】:

我在 Page.Resources 中有一个 DataTemplate,表示餐厅中表的结构及其相关编号:

<DataTemplate x:Key="Table">
    <RelativePanel x:Name="container" Height="40" Width="40">
        <TextBlock x:Name="number" RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True"/>
    </RelativePanel>
</DataTemplate>

在 XAML 中我使用 ContentControl,因为我必须重复相同的结构以避免复制粘贴:

<ContentControl ContentTemplate="{StaticResource Table}" Canvas.Top="200" Canvas.Left="100"/>
<ContentControl ContentTemplate="{StaticResource Table}" Canvas.Top="150" Canvas.Left="300"/>

然后我有一个按钮可以在代码隐藏中动态添加新的 ContentControl(Table)

private void NewTable(object sender, RoutedEventArgs e)
        {
            ContentControl tavolo = new ContentControl();
            tavolo.ContentTemplate = (DataTemplate)this.Resources["Table"];
            //here i Want to take the textblock called "number" and change his text.
            Canvas.SetTop(tavolo, 150);
            Canvas.SetLeft(tavolo, 150);
            sala.Children.Add(tavolo);
        }

但是如果我想在这个函数中更改名为“number”的文本块的文本(仅针对那个新表,而不是针对所有其他 ContentControl),我应该怎么做?使用数据绑定解决此问题的方法是什么?

此外,解决“复制粘贴”问题的最佳方法是什么?我应该使用像用户控件这样的其他结构吗? 我希望有人会回答所有这些问题。我真的很困惑和卡住了

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    如何通过 ContentControl 获取 DataTemplate 的子对象

    我发现您在代码隐藏中动态添加了ContentControl。对于这种情况,更好的方法是使用VisualTreeHelper 来查找孩子。

    public static DependencyObject MyFindChildByName(DependencyObject parant, string ControlName)
    {
        int count = VisualTreeHelper.GetChildrenCount(parant);
    
        for (int i = 0; i < count; i++)
        {
            var MyChild = VisualTreeHelper.GetChild(parant, i);
            if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == ControlName)
                return MyChild;
    
            var FindResult = MyFindChildByName(MyChild, ControlName);
            if (FindResult != null)
                return FindResult;
        }
    
        return null;
    }
    

    用法

    private void AppBar_Tapped(object sender, TappedRoutedEventArgs e)
    {
        ContentControl tavolo = new ContentControl();
        tavolo.ContentTemplate = (DataTemplate)this.Resources["Table"];
        //here i Want to take the textblock called "number" and change his text.
        Canvas.SetTop(tavolo, 150);
        Canvas.SetLeft(tavolo, 150);
        tavolo.Loaded += Tavolo_Loaded;  
        sala.Children.Add(tavolo);
    }
    
    private void Tavolo_Loaded(object sender, RoutedEventArgs e)
    {
        var textblcok = MyFindChildByName(sender as ContentControl, "number") as TextBlock;
        textblcok.Text = "New Content";
    }
    

    【讨论】:

    • 非常感谢您对我的帮助!它工作得很好,这是一个非常聪明的解决方案。但是现在我有另一个问题:您能告诉我如何从文本块“数字”开始获取 ContentControl 父级吗?一般来说,如何从孩子导航到父母
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多