嗨,我有少數幾個單一的文本框內datatemplate itemscontrol。當我將itemcontrol綁定到可觀察集合時,我得到兩個文本框。但我需要基於每個文本框做一些操作,我希望使用某些ID來分別查找每個文本框。尋找在WPF itemscontrol控件
任何人都可以幫助如何找到WPF中itemscontrol控件。
嗨,我有少數幾個單一的文本框內datatemplate itemscontrol。當我將itemcontrol綁定到可觀察集合時,我得到兩個文本框。但我需要基於每個文本框做一些操作,我希望使用某些ID來分別查找每個文本框。尋找在WPF itemscontrol控件
任何人都可以幫助如何找到WPF中itemscontrol控件。
使用ItemContainerGenerator,您可以獲取項目的生成容器並向下遍歷可視樹以找到您的TextBox。在一個ItemsControl的情況下,這將是一個ContentPresenter,但如果你想使用
itemsControl.ItemContainerGenerator.ContainerFromIndex(0);
一個ListBox會返回一個ListBoxItem的,一的ListView的ListViewItem等
ContentPresenter cp = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter;
TextBox tb = FindVisualChild<TextBox>(cp);
if (tb != null)
{
// do something with tb
}
public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
return (T)child;
}
T childItem = FindVisualChild<T>(child);
if (childItem != null) return childItem;
}
}
return null;
}
您也可以通過索引獲得容器
你可能想嘗試使用VisualTreeHelper。 ItemsControl本身的屬性只允許你獲取綁定的數據,而不是用於可視化數據的模板實例,而VisualTreeHelper允許你在WPF顯示時瀏覽可視樹。
如果您通過父級ItemControl的可視化子元素(遞歸地)遍歷,您不應該在屏幕上看到文本框。
感謝布萊斯,我試圖勾選向上的箭頭,但它說我的評分太低了!抱歉!
我修改了代碼,以返回給定類型的所有子代的所有列表,因爲這是我需要的,並且認爲別人可能會覺得它有用。
再次感謝布萊斯,真的很有幫助 - 抱歉評價的事情!
public static List<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
List<T> list = new List<T>();
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
list.Add((T)child);
}
List<T> childItems = FindVisualChildren<T>(child);
if (childItems != null && childItems.Count() > 0)
{
foreach (var item in childItems)
{
list.Add(item);
}
}
}
}
return list;
}
如果你有數據網格和模板列,其中包含數據的模板, 您可以使用下面的代碼示例
<DataGridTemplateColumn x:Name="photoPathColumn" Header="{x:Static resx:FrmResource.Photo}">
<DataGridTemplateColumn.CellEditingTemplate x:Uid="keyelm">
<DataTemplate x:Name="dodo">
<StackPanel Orientation="Horizontal" Height="Auto">
<TextBlock x:Name="photo" x:Uid="imageFile" Text="{Binding Path=PhotoPath}" />
<Button x:Name="Browse" Content="..." Click="Browse_Click" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
photoPathColumn.CellEditingTemplate.FindName("photo",photoPathColumn.GetCellContent(CustomersDataGrid.CurrentItem))
又如:
private void DataGridBank_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
try
{
switch (e.Key)
{
case Key.Down:
if ((DataGridBank.SelectedIndex + 1) <= DataGridBank.Items.Count)
{
DataGridBank.SelectedIndex = DataGridBank.SelectedIndex + 1;
FocusCell();
}
break;
case Key.Up:
if ((DataGridBank.SelectedIndex - 1) >= 0)
{
DataGridBank.SelectedIndex = DataGridBank.SelectedIndex - 1;
FocusCell();
}
break;
case Key.Enter:
case Key.Tab:
FocusCell();
break;
}
}
catch (Exception ex)
{
}
}
private void DataGridBank_Loaded(object sender, RoutedEventArgs e)
{
try
{
if (DataGridBank.Items.Count > 0)
{
DataGridBank.SelectedIndex = 0;
FocusCell();
}
}catch(Exception ex)
{
}
}
private void FocusCell()
{
var selectedRow = (DataGridRow)DataGridBank.ItemContainerGenerator.ContainerFromItem(DataGridBank.SelectedItem);
var textImport = FindVisualChild<TextBox>(selectedRow);
textImport.Focus();
textImport.SelectAll();
}
public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
return (T)child;
}
T childItem = FindVisualChild<T>(child);
if (childItem != null) return childItem;
}
}
return null;
}
做什麼樣的操縱你需要執行?您是否需要在itemscontrol的所有項目中唯一標識文本框,或者僅將兩者分開? – Oskar 2009-06-11 10:54:32
嗨奧斯卡 我只有標籤和文本框在我的itemscontrol裏面。我需要一種方法來獲得唯一的文本框控件的句柄(使用一些id)。 加載項目控件時需要將焦點放在文本框上,並需要確定哪些文本框在給定時間聚焦並執行某些操作。 謝謝 Deepak 謝謝 Deepak – deepak 2009-06-11 11:21:14
更簡單我如何迭代使用c#的itemscontrol控件集合。 – deepak 2009-06-11 12:49:41