第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從視圖模型(C#)對WPF中的TextBox設(shè)置焦點(diǎn)

從視圖模型(C#)對WPF中的TextBox設(shè)置焦點(diǎn)

C#
幕布斯6054654 2019-06-28 17:02:00
從視圖模型(C#)對WPF中的TextBox設(shè)置焦點(diǎn)我有一個TextBox和一個Button在我看來?,F(xiàn)在,我在單擊按鈕時檢查一個條件,如果條件結(jié)果為false,則向用戶顯示消息,然后我必須將光標(biāo)設(shè)置為TextBox控制室。if (companyref == null){     var cs = new Lipper.Nelson.AdminClient.Main.Views.ContactPanels.CompanyAssociation();      MessageBox.Show("Company does not exist.", "Error", MessageBoxButton.OK,                     MessageBoxImage.Exclamation);     cs.txtCompanyID.Focusable = true;     System.Windows.Input.Keyboard.Focus(cs.txtCompanyID);}上面的代碼在ViewModel中。這個CompanyAssociation視圖名。但是游標(biāo)沒有被設(shè)置在TextBox.XAML是:<igEditors:XamTextEditor Name="txtCompanyID"                           KeyDown="xamTextEditorAllowOnlyNumeric_KeyDown"                          ValueChanged="txtCompanyID_ValueChanged"                          Text="{Binding Company.CompanyId,                              Mode=TwoWay,                              UpdateSourceTrigger=PropertyChanged}"                          Width="{Binding ActualWidth, ElementName=border}"                          Grid.Column="1" Grid.Row="0"                          VerticalAlignment="Top"                          HorizontalAlignment="Stretch"                          Margin="0,5,0,0"                          IsEnabled="{Binding Path=IsEditable}"/><Button Template="{StaticResource buttonTemp1}"         Command="{Binding ContactCommand}"         CommandParameter="searchCompany"         Content="Search"         Width="80"         Grid.Row="0" Grid.Column="2"         VerticalAlignment="Top"         Margin="0"         HorizontalAlignment="Left"         IsEnabled="{Binding Path=IsEditable}"/>
查看完整描述

3 回答

?
長風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個贊

我知道這個問題已經(jīng)被回答了上千次,但我對安瓦卡的貢獻(xiàn)做了一些修改,我認(rèn)為這將幫助其他有類似問題的人。

首先,我更改了上述附加財(cái)產(chǎn)如下:

public?static?class?FocusExtension{
????public?static?readonly?DependencyProperty?IsFocusedProperty?=?
????????DependencyProperty.RegisterAttached("IsFocused",?typeof(bool?),?typeof(FocusExtension),?new?FrameworkPropertyMetadata(IsFocusedChanged){BindsTwoWayByDefault?=?true});

????public?static?bool??GetIsFocused(DependencyObject?element)
????{
????????if?(element?==?null)
????????{
????????????throw?new?ArgumentNullException("element");
????????}

????????return?(bool?)element.GetValue(IsFocusedProperty);
????}

????public?static?void?SetIsFocused(DependencyObject?element,?bool??value)
????{
????????if?(element?==?null)
????????{
????????????throw?new?ArgumentNullException("element");
????????}

????????element.SetValue(IsFocusedProperty,?value);
????}

????private?static?void?IsFocusedChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e)
????{
????????var?fe?=?(FrameworkElement)d;

????????if?(e.OldValue?==?null)
????????{
????????????fe.GotFocus?+=?FrameworkElement_GotFocus;
????????????fe.LostFocus?+=?FrameworkElement_LostFocus;
????????}

????????if?(!fe.IsVisible)
????????{
????????????fe.IsVisibleChanged?+=?new?DependencyPropertyChangedEventHandler(fe_IsVisibleChanged);
????????}

????????if?((bool)e.NewValue)
????????{
????????????fe.Focus();
????????}
????}

????private?static?void?fe_IsVisibleChanged(object?sender,?DependencyPropertyChangedEventArgs?e)
????{
????????var?fe?=?(FrameworkElement)sender;
????????if?(fe.IsVisible?&&?(bool)((FrameworkElement)sender).GetValue(IsFocusedProperty))
????????{
????????????fe.IsVisibleChanged?-=?fe_IsVisibleChanged;
????????????fe.Focus();
????????}
????}

????private?static?void?FrameworkElement_GotFocus(object?sender,?RoutedEventArgs?e)
????{
????????((FrameworkElement)sender).SetValue(IsFocusedProperty,?true);
????}

????private?static?void?FrameworkElement_LostFocus(object?sender,?RoutedEventArgs?e)
????{
????????((FrameworkElement)sender).SetValue(IsFocusedProperty,?false);
????}}

我添加可見性引用的原因是選項(xiàng)卡。顯然,如果您在初始可見選項(xiàng)卡之外的任何其他選項(xiàng)卡上使用附加屬性,則附加屬性在手動聚焦控件之前無法工作。

另一個障礙是創(chuàng)建一種更優(yōu)雅的方法,當(dāng)基礎(chǔ)屬性失去焦點(diǎn)時,將其重置為false。這就是失去焦點(diǎn)事件出現(xiàn)的原因。

<TextBox????????????
????Text="{Binding?Description}"
????FocusExtension.IsFocused="{Binding?IsFocused}"/>



查看完整回答
反對 回復(fù) 2019-06-28
  • 3 回答
  • 0 關(guān)注
  • 1350 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號