您的位置:首页 > 其它

怎样控制WPF GroupBox.HeaderTemplate中的控件

2009-07-20 22:06 447 查看
Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<StackPanel>
<GroupBox Name="GroupBoxUC" MinHeight="100">
<GroupBox.HeaderTemplate>
<DataTemplate>
<DockPanel Name="dockbutton" HorizontalAlignment="Right">
<TextBlock Name="txtHeader" Text="header" Foreground="Red"/>
<Button MinHeight="20" Width="50" Content="后 退" Click="btnBack_Click" Name="btnBack"/>
<Button MinHeight="20" Width="50" Content="前 进" Click="btnForward_Click" Name="btnForward"/>
</DockPanel>
</DataTemplate>
</GroupBox.HeaderTemplate>
</GroupBox>
</StackPanel>
</Grid>
</Window>

------------------------------------------------------

Window1.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfApplication1
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
private Button btnBack;
private Button btnForward;
private TextBlock txtheader;

public Window1()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Border headborder = (Border)GroupBoxUC.Template.FindName("Header", GroupBoxUC);
ContentPresenter headContentPresenter = (ContentPresenter)headborder.Child;

DataTemplate datatemp = GroupBoxUC.HeaderTemplate;

txtheader = (TextBlock)datatemp.FindName("txtHeader", headContentPresenter);
btnBack = datatemp.FindName("btnBack", headContentPresenter) as Button;
btnForward = datatemp.FindName("btnForward", headContentPresenter) as Button;

}

private void btnBack_Click(object sender, RoutedEventArgs e)
{
txtheader.Text = "后退!";

btnBack.IsEnabled = false;
btnForward.IsEnabled = true;
}

private void btnForward_Click(object sender, RoutedEventArgs e)
{
txtheader.Text = "前进!";

btnForward.IsEnabled = false;
btnBack.IsEnabled = true;
}

}
}

------------------------------------------------------------------------------

源程序下载

资料:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d8fd2289-d8b9-447f-8d04-9557bd3ccda3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: