如何將焦點(從后面的 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>
順便說一句:單擊按鈕時會生成一個新專案。
謝謝!
uj5u.com熱心網友回復:
您可以使用此附加屬性:
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>
uj5u.com熱心網友回復:
感謝您的建議,但它們對我沒有用。
但它可以使用 FocusManager:
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
另請參閱:在 xaml wpf 中將焦點設定在文本框上
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489038.html
