wpf样式问题
请教大神, 为什么在下面红色代码设置样式时,点击按钮可以获取样式中的combobox,在蓝色代码设置时,获取的是null。
如果需要在蓝色代码设置时可以获取到样式中的combobox,要怎么写代码
xaml代码
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="style1" TargetType="DataGridColumnHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ComboBox Name="cb">
<ComboBoxItem>123123</ComboBoxItem>
<ComboBoxItem>1aa</ComboBoxItem>
</ComboBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid >
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="0.5*"></RowDefinition>
</Grid.RowDefinitions>
<DataGrid Name="dg" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="dg1" Binding="{Binding name}" ></DataGridTextColumn>
<DataGridTextColumn Header="dg2" Binding="{Binding id}"></DataGridTextColumn>
<DataGridTextColumn Header="dg3" Binding="{Binding age}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Button Name="btn" Click="btn_Click" Grid.Row="1">btn</Button>
</Grid>
</Window>
.cs代码
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<Person> collection = new ObservableCollection<Person>();
public MainWindow()
{
InitializeComponent();
for(int i = 0 ;i < 6 ;i++)
{
Person ps = new Person();
ps.name = "name : " + i ;
ps.id = "id : " + i;
ps.age = "age : " + i;
ps.sex = "sex : " + i;
collection.Add(ps);
}
DataGridTextColumn dgt = new DataGridTextColumn();
dgt.Header = "dg1";
Binding bi = new Binding("name");
dgt.Binding = bi;
dg.Columns.Add(dgt);
dg.ItemsSource = collection;
dg.Columns[0].HeaderStyle = Resources["style1"] as Style;
}
private void btn_Click(object sender, RoutedEventArgs e)
{
//dg.Columns[0].HeaderStyle = Resources["style1"] as Style;
var child = FindChild<ComboBox>(dg);
if (null == child)
{
}
}
public T FindChild<T>(DependencyObject parent) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent) ;i++ )
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child != null && child is T)
{
return child as T ;
}
else
{
T childofchild = FindChild<T>(child);
if (null != childofchild )
{
return childofchild;
}
}
}
return null;
}
}
public class Person : INotifyPropertyChanged
{
private string _name = string.Empty;
public string name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("name");
}
}
private string _id = string.Empty;
public string id
{
get { return _id; }
set
{
_id = value;
OnPropertyChanged("id");
}
}
private string _age = string.Empty;
public string age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged("age");
}
}
private string _sex = string.Empty;
public string sex
{
get { return _sex; }
set
{
_sex = value;
OnPropertyChanged("sex");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}