简体   繁体   English

如何设置ControlTemplate的子项的属性?

[英]How to set properties of ControlTemplate's children?

I have ControlTemplate control, it's x:Name="MyControlTemplate" . 我有ControlTemplate控件,它是x:Name="MyControlTemplate" It contains ListBox it's x:Name="MyListBox" . 它包含ListBox它是x:Name="MyListBox"

Then in my XAML code I have: 然后在我的XAML代码中,我有:

<ContentControl Template="{StaticResource MyControlTemplate}" x:Name="MyContentControl"/>

I need to assign ItemsSource property to MyListBox in C# code of MainPage.xaml.cs . 我需要在MainPage.xaml.cs C#代码中将ItemsSource属性分配给MyListBox

How can I do it? 我该怎么做?

First you need to find the list box then just set the property. 首先,您需要找到列表框,然后只需设置属性。

ListBox listBox = (ListBox) MyContentControl.Template.FindName("MyListBox", MyContentControl);
listBox.ItemsSource = new List<string>() { "1", "2", "3" };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM