轉帖|其它|編輯:郝浩|2010-12-31 11:49:22.000|閱讀 843 次
概述:有這樣一個需求,當用戶雙擊Tab控件Header區域時, 希望可以直接編輯。對于WPF控件,提供一個ControlTemplate在加上一些Trigger就可以實現。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
有這樣一個需求,當用戶雙擊Tab控件Header區域時, 希望可以直接編輯。對于WPF控件,提供一個ControlTemplate在加上一些Trigger就可以實現。效果如下:
代碼
首先,我們需要給Tab Header設計一個ControlTemplate。類似一個TextBlock,雙擊進入編輯狀態。 所以Xaml如下:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:EditableTabHeaderControl}">
<Grid>
<TextBox x:Name="PART_TabHeader" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay}" Visibility="Collapsed"/>
<TextBlock x:Name="PART_TextBlock" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsInEditMode" Value="True">
<Trigger.Setters>
<Setter TargetName="PART_TabHeader" Property="Visibility" Value="Visible"/>
<Setter TargetName="PART_TextBlock" Property="Visibility" Value="Collapsed"/>
</Trigger.Setters>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
接下來,我們需要定義個“EditableTabHeaderControl”類,它具有控制TextBox和TextBlock的能力。如下:
namespace EditableTabHeaderDemo
{
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
/// <summary>
/// Header Editable TabItem
/// </summary>
[TemplatePart(Name = "PART_TabHeader", Type = typeof(TextBox))]
public class EditableTabHeaderControl : ContentControl
{
/// <summary>
/// Dependency property to bind EditMode with XAML Trigger
/// </summary>
private static readonly DependencyProperty IsInEditModeProperty = DependencyProperty.Register("IsInEditMode", typeof(bool), typeof(EditableTabHeaderControl));
private TextBox textBox;
private string oldText;
private DispatcherTimer timer;
private delegate void FocusTextBox();
/// <summary>
/// Gets or sets a value indicating whether this instance is in edit mode.
/// </summary>
public bool IsInEditMode
{
get
{
return (bool)this.GetValue(IsInEditModeProperty);
}
set
{
if (string.IsNullOrEmpty(this.textBox.Text))
{
this.textBox.Text = this.oldText;
}
this.oldText = this.textBox.Text;
this.SetValue(IsInEditModeProperty, value);
}
}
/// <summary>
/// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.textBox = this.Template.FindName("PART_TabHeader", this) as TextBox;
if (this.textBox != null)
{
this.timer = new DispatcherTimer();
this.timer.Tick += TimerTick;
this.timer.Interval = TimeSpan.FromMilliseconds(1);
this.LostFocus += TextBoxLostFocus;
this.textBox.KeyDown += TextBoxKeyDown;
this.MouseDoubleClick += EditableTabHeaderControlMouseDoubleClick;
}
}
/// <summary>
/// Sets the IsInEdit mode.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
public void SetEditMode(bool value)
{
this.IsInEditMode = value;
this.timer.Start();
}
private void TimerTick(object sender, EventArgs e)
{
this.timer.Stop();
this.MoveTextBoxInFocus();
}
private void MoveTextBoxInFocus()
{
if (this.textBox.CheckAccess())
{
if (!string.IsNullOrEmpty(this.textBox.Text))
{
this.textBox.CaretIndex = 0;
this.textBox.Focus();
}
}
else
{
this.textBox.Dispatcher.BeginInvoke(DispatcherPriority.Render, new FocusTextBox(this.MoveTextBoxInFocus));
}
}
private void TextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
this.textBox.Text = oldText;
this.IsInEditMode = false;
}
else if (e.Key == Key.Enter)
{
this.IsInEditMode = false;
}
}
private void TextBoxLostFocus(object sender, RoutedEventArgs e)
{
this.IsInEditMode = false;
}
private void EditableTabHeaderControlMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.SetEditMode(true);
}
}
}
}
這里有一個問題,當控件進入編輯狀態,TextBox變為可見狀態時,它不能自動獲得focus。一種解決辦法是掛一個Timer,每1毫秒輪詢一次,檢查狀態并控制focus。
現在就來添加一個WPF TabControl,并應用ItemContainerStyle。然后雙擊Header,可以編輯啦~
<Window x:Class="EditableTabHeaderDemo.MainWindow"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:EditableTabHeaderDemo"
Title="EditableTabHeaderDemo" Height="300" Width="500">
<Window.Resources>
<Style x:Key="EditableTabHeaderControl" TargetType="{x:Type local:EditableTabHeaderControl}">
<!-- The template specified earlier will come here !-->
</Style>
<Style x:Key="ItemContainerStyle" TargetType="TabItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<local:EditableTabHeaderControl
Style="{StaticResource EditableTabHeaderControl}">
<local:EditableTabHeaderControl.Content>
<Binding Path="Name" Mode="TwoWay"/>
</local:EditableTabHeaderControl.Content>
</local:EditableTabHeaderControl>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="ContentTemplate">
<Grid>
<TextBlock HorizontalAlignment="Left" Text="{Binding Name}"/>
<TextBlock HorizontalAlignment="Center" Text="{Binding City}"/>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl Grid.Row="0" ItemsSource="{Binding Data}" ItemContainerStyle="{StaticResource ItemContainerStyle}" ContentTemplate="{StaticResource ContentTemplate}" />
</Grid>
</Window>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載