gpt4 book ai didi

.net - WPF MVVM设置控制焦点

转载 作者:行者123 更新时间:2023-12-03 10:41:14 25 4
gpt4 key购买 nike

您如何在MVVM中设置用户控制焦点?使用Focusmanager.FocusElement = {Binding ...}没有影响。

这是我的XAML:

<DataTemplate DataType="{x:Type client:TelephoneNumberViewModel}">
<Grid FocusManager.FocusedElement="{Binding ElementName=TelephoneNumber}" Width="1024" Height="540">
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<TextBlock Margin="0 25 0 0" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="1"
Grid.ColumnSpan="7" Name="textBlockQuestion"
TextWrapping="Wrap" Style="{DynamicResource TitleTextBlock}">"What is your telephone number?"</TextBlock>

<Grid Grid.Column="1" Grid.Row="1" Grid.RowSpan="7"
Grid.ColumnSpan="7" Height="460">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>

<StackPanel Grid.Row="0" Grid.RowSpan="5" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Top" HorizontalAlignment="Left">
<wpfclient:TelephoneBox Name="TelephoneNumber" TelephoneNumber="{Binding PhoneNumber, Mode=TwoWay}" />
<!--<TextBox Width="500" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="40" TextAlignment="Center" Text="{Binding PhoneNumber}"></TextBox>-->

<StackPanel Orientation="Horizontal" Margin="0 40 0 0">
<StackPanel Orientation="Vertical" Margin="0 0 60 0" >
<Button Margin="0 0 0 20" Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyMobileCommand}">Mobile</Button>
<Button Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyHomeCommand}">Home</Button>

</StackPanel>
<StackPanel Orientation="Vertical">
<Button Margin="0 0 0 20" Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyWorkCommand}">Work</Button>
<Button Style="{DynamicResource LargeGlossyButtonStyle}" Command="{Binding SurveyOtherCommand}">Other</Button>
</StackPanel>
</StackPanel>



</StackPanel>


</Grid>



<Grid Width="1024" Height="80" Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="8" VerticalAlignment="Bottom">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Margin="60 0 0 20" Grid.Column="0" Grid.Row="0" Name="buttonContinue"
Command="{Binding SurveySkipCommand}" Style="{DynamicResource LargeGlossyButtonStyle}"
>Skip</Button>
</Grid>



</Grid>
</DataTemplate>

用户控件只有在我一次按Tab键后才能获得焦点。

最佳答案

使用 FocusManager.FocusedElement

在编辑后进行编辑:我认为不看完整的代码就不可能说,但我认为这可能是焦点范围的问题。您正在将逻辑焦点设置为电话控件,但是如果该逻辑范围是 Activity 的,则仅具有键盘焦点。我怀疑控件之外的某些东西有自己的焦点范围,直到您单击选项卡,范围才移到UserControl中。

关于.net - WPF MVVM设置控制焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/772772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com