【发布时间】: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),我应该怎么做?使用数据绑定解决此问题的方法是什么?
此外,解决“复制粘贴”问题的最佳方法是什么?我应该使用像用户控件这样的其他结构吗? 我希望有人会回答所有这些问题。我真的很困惑和卡住了
【问题讨论】: