WinUi3如何获取dataTemplate中的元素
WinUi3 how to get Elements inside dataTemplate
这是我的xaml代码,一个NavigationView
包含一个NavigationView.HeaderTemplate
。
<NavigationView.HeaderTemplate>
<DataTemplate
x:Name="HeaderDataTemplate">
<TextBlock
Name="HeaderText"
Text="I am a TextBlock"
Margin="0,0,0,10"/>
</DataTemplate>
</NavigationView.HeaderTemplate>
我想知道如何通过 C# 代码获取 Texkblock
。
这是我的 C# 代码,但它不起作用。
private void nvSample_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
var navView = sender as NavigationView;
NavigationViewItem item = args.SelectedItemContainer as NavigationViewItem;
if(item.Tag.ToString() == "SatrtPage")
{
DataTemplate dataTemplate = sender.HeaderTemplate;
DependencyObject dio = dataTemplate.LoadContent();
var rectangle= FindVisualChild<TextBlock>(dio);
if (rectangle != null)
{
rectangle.Text = "LLLLLLL";
}
contentFrame.Navigate(typeof(StartPage));
}
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
{
return (childItem)child;
}
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
与其通过代码操作 UI,不如考虑使用 MVVM 模式并将属性绑定到模型,然后您可以直接访问模型,视图将自行更新。
试试这个:
private void nvSample_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
var navView = sender as NavigationView;
if (FindVisualChild<SplitView>(navView) is SplitView splitView
&& splitView.Content is Panel panel
&& panel.Children.OfType<ContentControl>().FirstOrDefault() is ContentControl cc)
{
var textBlock = FindVisualChild<TextBlock>(cc);
if (textBlock != null)
textBlock.Text = "changed text...";
}
}
这是我的xaml代码,一个NavigationView
包含一个NavigationView.HeaderTemplate
。
<NavigationView.HeaderTemplate>
<DataTemplate
x:Name="HeaderDataTemplate">
<TextBlock
Name="HeaderText"
Text="I am a TextBlock"
Margin="0,0,0,10"/>
</DataTemplate>
</NavigationView.HeaderTemplate>
我想知道如何通过 C# 代码获取 Texkblock
。
这是我的 C# 代码,但它不起作用。
private void nvSample_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
var navView = sender as NavigationView;
NavigationViewItem item = args.SelectedItemContainer as NavigationViewItem;
if(item.Tag.ToString() == "SatrtPage")
{
DataTemplate dataTemplate = sender.HeaderTemplate;
DependencyObject dio = dataTemplate.LoadContent();
var rectangle= FindVisualChild<TextBlock>(dio);
if (rectangle != null)
{
rectangle.Text = "LLLLLLL";
}
contentFrame.Navigate(typeof(StartPage));
}
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
{
return (childItem)child;
}
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
与其通过代码操作 UI,不如考虑使用 MVVM 模式并将属性绑定到模型,然后您可以直接访问模型,视图将自行更新。
试试这个:
private void nvSample_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
var navView = sender as NavigationView;
if (FindVisualChild<SplitView>(navView) is SplitView splitView
&& splitView.Content is Panel panel
&& panel.Children.OfType<ContentControl>().FirstOrDefault() is ContentControl cc)
{
var textBlock = FindVisualChild<TextBlock>(cc);
if (textBlock != null)
textBlock.Text = "changed text...";
}
}