问题描述
我在 C#/WPF应用程序中有一个xaml屏幕,其中包含一个组合框(产品类型)和一个文本框(产品代码).
首次加载屏幕时,我将使用下面的代码来设置此文本框的焦点及其工作正常.
当用户更改comboxbox中的值但它似乎不起作用时,我还需要设置焦点.
我在这里想念什么吗?
(注意:我的首要选择是使用MVVM设计模式来解决此问题.如果不起作用,请使用代码隐藏方法.)
** MainWindowResources.xaml **
< Style TargetType =" TextBox" x:Key ="ProductCodeStyle">
< Style.Triggers>
< DataTrigger Binding =" {Binding FocusOnProductCode}"值="True">
< Setter Property =" FocusManager.FocusedElement"值="{Binding RelativeSource = {RelativeSource Self}}" />
</DataTrigger>
</Style.Triggers>
</Style>
** MainWindow.xaml:**
< TextBox Name =" txtProductCode" HorizontalAlignment =左"高度="22". TextWrapping ="Wrap" Text ="{Binding ProductCodeValue,NotifyOnSourceUpdated = True,UpdateSourceTrigger = PropertyChanged}""
VerticalAlignment =顶部"宽度="165". Style ="{DynamicResource ProductCodeStyle}" Grid.Column ="3".保证金="1、2、0、0". TabIndex ="0". IsHitTestVisible =真"/>
** MainWindowViewModel.cs **
公共MainWindowViewModel(MainWindow窗口)
{
this.FocusOnProductCode = true;
}
public ProductType SelectedProductType
{
得到
{
返回m_selectedProductType;
}
设置
{
m_selectedProductType =值;
this.FocusOnProductCode = true;
}
}
公共布尔FocusOnProductCode
{
得到{return m_focusOnProductCode; }
设置
{
m_focusOnProductCode; =值;
OnPropertyChanged("FocusOnProductCode");
OnPropertyChanged("SelectedProductType");
}
}
谢谢
I've a xaml screen in my C#/WPF app having a combobox(ProductType) and a textbox(ProductCode).
When the screen loads for the first time,I'm setting the focus this Textbox and its working fine using my code below.
I also need to set the focus when user changes a value in the comboxbox but it does not seem to work.
What am I missing here please?
(Note:My first preference would be to achieve a solution for this using MVVM design pattern.If it does not work,I would like to go for code-behind approach please.)
**MainWindowResources.xaml**
<Style TargetType="TextBox" x:Key="ProductCodeStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding FocusOnProductCode}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
</DataTrigger>
</Style.Triggers>
</Style>
**MainWindow.xaml:**
<TextBox Name="txtProductCode" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" Text="{Binding ProductCodeValue, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged }"
VerticalAlignment="Top" Width="165" Style="{DynamicResource ProductCodeStyle}" Grid.Column="3" Margin="1,2,0,0" TabIndex="0" IsHitTestVisible="True"/>
**MainWindowViewModel.cs**
public MainWindowViewModel(MainWindow window)
{
this.FocusOnProductCode = true;
}
public ProductType SelectedProductType
{
get
{
return m_selectedProductType;
}
set
{
m_selectedProductType = value;
this.FocusOnProductCode = true;
}
}
public bool FocusOnProductCode
{
get { return m_focusOnProductCode; }
set
{
m_focusOnProductCode;= value;
OnPropertyChanged("FocusOnProductCode");
OnPropertyChanged("SelectedProductType");
}
}
Thanks.
推荐答案
您在初始化中使用什么代码?这些代码不起作用吗?
What code do you use in the initialization, and is the code the same that does not work?
这篇关于将下拉列表值更改设置为文本框上的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!