wpf样式问题

zhihaozwj 2017-09-07 08:33:56
请教大神, 为什么在下面红色代码设置样式时,点击按钮可以获取样式中的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));
}
}
}
}
...全文
1230 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq14923349 2017-09-21
  • 打赏
  • 举报
回复
引用 6 楼 sudazf 的回复:
为什么我在蓝色代码设置时,获取的不是null呢
资源加载顺序和事件
晚安苏州 2017-09-11
  • 打赏
  • 举报
回复
为什么我在蓝色代码设置时,获取的不是null呢
zhihaozwj 2017-09-09
  • 打赏
  • 举报
回复
动态增加列,并且给该列的headerstyle赋予设定好的样式,同时给样式中的combobox动态赋值,
zhihaozwj 2017-09-08
  • 打赏
  • 举报
回复
在点击按钮的时候赋给style, 然后获取style中的combobx,这没什么矛盾的吧
exception92 2017-09-08
  • 打赏
  • 举报
回复
点击按钮可以获取样式中的combobox,在蓝色代码设置时,获取的是null。 如果需要在蓝色代码设置时可以获取到样式中的combobox,要怎么写代码 说的矛盾吧
Veary 2017-09-08
  • 打赏
  • 举报
回复
有点听不懂你说的是什么 但是我比较确定的一点是 想要获取类似这种模板啊、样式啊的东西,一定要在窗体Loaded之后,或者在你要获取的那个控件Loaded之后
exception92 2017-09-08
  • 打赏
  • 举报
回复
引用 2 楼 zhihaozwj 的回复:
在点击按钮的时候赋给style, 然后获取style中的combobx,这没什么矛盾的吧
.HeaderStyle? 最终要对combobox进行什么操作

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧