How to get the control inside the ControlTemplate/DataTemplate on run time.
Here is a similar example to get control inside DataTemplate at runtime.
XAML code:
<StackPanel>
<StackPanel.Resources >
<local:People x:Key="mydata"/>
</StackPanel.Resources>
<ComboBox ItemsSource="{StaticResource mydata}" Name="myComboBox">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="txtName" Text="{Binding Path=Name}" GotFocus="textBoxGetFocus"/>
<TextBox Name="txtID" Text="{Binding Path=ID}" GotFocus="textBoxGetFocus"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Width="250" Margin="20,100,20,20" Content="Get name from DataTemplate and change it" Click="btnChangeIt" ></Button>
</StackPanel>
In the code behind:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
ComboBoxItem item;
// the index is the first item by default
int index=0;
void textBoxGetFocus(object sender, RoutedEventArgs e)
{
TextBox text = e.Source as TextBox;
myComboBox.SelectedItem = text.DataContext;
// get the selected item's index
index = myComboBox.Items.IndexOf(myComboBox.SelectedItem);
}
void btnChangeIt(object sender, RoutedEventArgs e)
{
item = myComboBox.ItemContainerGenerator.ContainerFromIndex(index) as ComboBoxItem;
if (item != null)
{
// get the selected combobox item's Parenttemplate
ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(item);
//get teh datatemplate
DataTemplate dataTemplate = myComboBox.ItemTemplate;
if (dataTemplate != null && templateParent != null)
{
TextBox textbox = dataTemplate.FindName("txtName", templateParent) as TextBox;
MessageBox.Show("Get the name value: "+textbox.Text);
}
}
}
private static T GetFrameworkElementByName<T>(FrameworkElement referenceElement) where T : FrameworkElement
{
FrameworkElement child = null;
//travel the visualtree by VisualTreeHelper to get the template
for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++)
{
child = VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement;
System.Diagnostics.Debug.WriteLine(child);
if (child != null && child.GetType() == typeof(T))
{ break; }
else if (child != null)
{
child = GetFrameworkElementByName<T>(child);
if (child != null && child.GetType() == typeof(T))
{
break;
}
}
}
return child as T;
}
}
public class People : System.Collections.ObjectModel.ObservableCollection<person>
{
public People()
{
this.Add(new person() { Name="name1",ID="ID1"});
this.Add(new person() { Name = "name2", ID = "ID2" });
this.Add(new person() { Name = "name3", ID = "ID3" });
}
}
public class person
{
public string Name { get; set; }
public string ID { get; set; }
}
还有用:
[TemplatePart(Name = "PART_DropDown", Type = typeof(Button))]
[ContentProperty("Items")]
[DefaultProperty("Items")]
public class SplitButton : Button
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
// set up the event handlers
ButtonBase dropDown = this.Template.FindName("PART_DropDown", this) as ButtonBase;
ContentPresenter contentpresenter = this.Template.FindName("contenPesenter", this) as ContentPresenter;
if (dropDown != null)
dropDown.Click += DoDropdownClick;
dropDown.IsEnabled = false;
}
}
{