介紹
有這樣乙個需求,當使用者雙擊tab控制項header區域時, 希望可以直接編輯。對於wpf控制項,提供乙個controltemplate在加上一些trigger就可以實現。效果如下:
**首先,我們需要給tab header設計乙個controltemplate。類似乙個textblock,雙擊進入編輯狀態。 所以xaml如下:
<setter
property
="template"
>
<
setter.value
>
<
controltemplate
targettype=""
>
<
grid
>
<
textbox
x:name
="part_tabheader"
text
=", path=content, mode=twoway}"
visibility
="collapsed"
/>
<
textblock
x:name
="part_textblock"
text
=", 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 editabletabheaderdemoset
this
this.oldtext
= this.textbox.text;
this.setvalue(isineditmodeproperty, value);
} }
/// <
summary
>
seecref
=/>
. ///
summary
> }
/// <
summary
>
/// sets the isinedit mode.
///
summary
>
/// <
param
name
="value"
>
if set to
<
c>
true
c>
[value].
param
>
public void seteditmode(bool value)
private void timertick(object sender, eventargs e)
private void movetextboxinfocus() }
else }
private void textboxkeydown(object sender, keyeventargs e)
else if (e.key
== key.enter) }
private void textboxlostfocus(object sender, routedeventargs e)
private void editabletabheadercontrolmousedoubleclick(object sender, mousebuttoneventargs e) }
} }
這裡有乙個問題,當控制項進入編輯狀態,textbox變為可見狀態時,它不能自動獲得focus。一種解決辦法是掛乙個timer,每1毫秒輪詢一次,檢查狀態並控制focus。
現在就來新增乙個wpf tabcontrol,並應用itemcontainerstyle。然後雙擊header,可以編輯啦~
<window
x:class
="editabletabheaderdemo.mainwindow"
xmlns
=""xmlns:x
=""xmlns:local
="clr-namespace:editabletabheaderdemo"
title
="editabletabheaderdemo"
height
="300"
width
="500"
>
<
window.resources
>
<
style
x:key
="editabletabheadercontrol"
targettype=""
>
style
>
<
style
x:key
="itemcontainerstyle"
targettype
="tabitem"
>
<
setter
property
="headertemplate"
>
<
setter.value
>
<
datatemplate
>
<
local:editabletabheadercontrol
style=""
>
<
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=""
/>
<
textblock
horizontalalignment
="center"
text=""
/>
grid
>
datatemplate
>
window.resources
>
<
grid
>
<
tabcontrol
grid.row
="0"
itemssource=""
itemcontainerstyle=""
contenttemplate=""
/>
grid
>
window
>
許可證本文以及示例**檔案遵循the code project open license(cpol)。
editabletabheadersolution.zip
WPF下可編輯Header的Tab控制項實現
介紹 接下來,我們需要定義個 editabletabheadercontrol 類,它具有控制textbox和textblock的能力。如下 namespace editabletabheaderdemo setthis.oldtext this.textbox.text this.setvalue...
可編輯子項的CListCtrl類
一 說明 大家都知道在mfc中通過給clistctrl設定lvs editlabels屬性,並且在程式中響應控制項的lvn endlabeledit訊息可以修改列表控制項每一行的第一項,也就是主項 item 如下 void ceditlistctrlsampledlg onendlabeleditl...
可編輯子項的CListCtrl類
大家都知道在mfc中通過給clistctrl設定lvs editlabels屬性,並且在程式中響應控制項的lvn endlabeledit訊息可以修改列表控制項每一行的第一項,也就是主項 item void ceditlistctrlsampledlg onendlabeleditlist1 nmh...