![](/img/trans.png)
[英]How to set the taborder for the Datatemplate textboxes in a listbox item?
[英]How is it possible to set the focus on the last item of a DataTemplate?
如何將焦點(從后面的 c#-code 或 xaml 本身)設置到我的 DataTemplate 的最后一項?
那是我的 XAML 代碼:
<ItemsControl x:Name="ListViewItems">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="1 10 0 10"
Height="60">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"
x:Name="SsidTextBox"
Text="{Binding Ssid, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" >
</TextBox>
<TextBox Grid.Column="1"
x:Name="PasswordTextBox"
Text="{Binding Password, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
順便說一句:單擊按鈕時會生成一個新項目。
謝謝!
您可以使用此附加屬性:
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool) obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof (bool), typeof (FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement) d;
if ((bool) e.NewValue)
{
uie.Focus(); // Don't care about false values.
}
}
}
然后將其添加到您要關注的元素中:
<TextBox Grid.Column="0" local:FocusExtension.IsFocused="{Binding SsidFocus}" x:Name="SsidTextBox" Text="{Binding Ssid, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" ></TextBox>
感謝您的建議,但它們對我沒有用。
但它可以使用 FocusManager:
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
另請參閱: 在 xaml wpf 中將焦點設置在文本框上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.