您可以使用IsReadOnly
而不是IsEnabled
來實現您正在查找的內容。另外,你可以很容易地在代碼中設置tapped事件處理程序。如上所述,我不確定是否需要在代碼中設置處理程序才能正常工作。然而,它確實簡化了一些事情。
這是細節。
在你的網頁類的構造函數(這裏是MainPage
),調出設置功能:
public MainPage()
{
this.InitializeComponent();
// call the setup for the textboxes
SetupTextBoxes();
}
這裏是我們做的魔術 - 使這個頁面只讀上的所有文本框,並設置了自來水處理:
private void SetupTextBoxes()
{
var tbs = GetVisualChildren<TextBox>(this, true);
foreach (var tb in tbs)
{
tb.IsReadOnly = true;
tb.AddHandler(TappedEvent, new TappedEventHandler(tb_Tapped), true);
}
}
效用函數來獲取父傳遞的給定類型(T
)的所有兒童的名單。
private List<T> GetVisualChildren<T>(DependencyObject parent, bool recurse = true)
where T : DependencyObject
{
var children = new List<T>();
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i);
var child = v as T;
if (child == null && recurse)
{
var myChildren = GetVisualChildren<T>(v, recurse);
children.AddRange(myChildren);
}
if (child != null)
children.Add(child);
}
return children;
}
最後,事件處理程序。這會在點擊時啓用每個文本框。
private void tb_Tapped(object sender, TappedRoutedEventArgs e)
{
((TextBox)(sender)).IsReadOnly = false;
}
該sorta工程。謝謝。我可以使用只讀字段,然後隨時爲外觀和字段設置主題畫筆。所以,沒關係我認爲 – RallyRabbit 2013-03-20 11:50:00
@RallyRabbit - 很高興爲你工作。不要忘記接受答案。 – 2013-03-20 13:23:19