unable to set focus to datepicker using attached property in wpf

≡放荡痞女 提交于 2020-01-15 04:19:13

问题


I have a custom datepicker with me and i am trying to set focus to this using attached property. I have the toolkit version 3.5.50211.1 which was released on Feb 2010. The focus is working fine when i do DatePicker.Focus(); in my code. My custom datepicker code is as follows:

-- My DatePicker

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
using System.Windows.Controls.Primitives;

namespace Guardian.PAS.PASFramework.UI.WPF
{
    public class PASDatePicker : DatePicker
    {
        #region Attached Property

        /// <summary>
        /// Get of IsFocus Property.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool GetIsFocus(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsFocusProperty);
        }

        /// <summary>
        /// Set of IsFocus Property.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="value"></param>
        public static void SetIsFocus(DependencyObject obj, bool value)
        {
            obj.SetValue(IsFocusProperty, value);
        }


        public static readonly DependencyProperty IsFocusProperty =
                DependencyProperty.RegisterAttached(
                 "IsFocus", typeof(bool), typeof(PASDatePicker),
                 new UIPropertyMetadata(false, OnIsFocusPropertyChanged));

        /// <summary>
        /// Property change event of IsFocused.
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnIsFocusPropertyChanged(DependencyObject d,
                DependencyPropertyChangedEventArgs e)
        {
            var uie = (UIElement)d;
            if ((bool)e.NewValue)
            {
                // Don't care about false values. 
                (uie as PASDatePicker).Focus();
            }
        }
        #endregion
    }
}

I am using this datepicker in my code and trying to set focus using the Isfocus attached property defined above.

-- test form xaml

<Window x:Class="Guardian.PAS.BARSLibrary.Test.frmPASDatePickerTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="frmPASDatePickerTest" Height="300" Width="300"
    xmlns:my="clr-namespace:Guardian.PAS.PASFramework.UI.WPF;assembly=Guardian.PAS.PASFramework.UI.WPF">
    <Grid>
        <my:PASDatePicker Name="dtpDate" Height="25" Margin="64,40,64,0" VerticalAlignment="Top"
        my:PASDatePicker.IsFocus ="{Binding GoviddateissuedFocused}" />
        <Button Height="23" Margin="64,90,139,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">change</Button>
    </Grid>
</Window>

-- Test form xaml.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;
using Microsoft.Windows.Controls;
using System.Configuration;
using System.Data;
using System.Collections;
using System.ComponentModel;

namespace Guardian.PAS.BARSLibrary.Test
{
    /// <summary>
    /// Interaction logic for frmPASDatePickerTest.xaml
    /// </summary>
    public partial class frmPASDatePickerTest : Window, INotifyPropertyChanged
    {
        public frmPASDatePickerTest()
        {
            InitializeComponent();

            dtpDate.Focus();
        }

        private bool _goviddateissuedFocused = false;
        /// <summary>
        /// Get/Set for GoviddateissuedFocused property.
        /// </summary>
        public bool GoviddateissuedFocused
        {
            get { return _goviddateissuedFocused; }
            set
            {
                if (_goviddateissuedFocused != value)
                {
                    _goviddateissuedFocused = value;
                    RaisePropertyChanged("GoviddateissuedFocused");
                }
            }
        }

        #region PropertyChanged Block
        /// <summary>
        /// EventHandler for Property Change
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises event on Property Change
        /// </summary>
        /// <param name="property"></param>
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            GoviddateissuedFocused = true;
        }
    }
}

Now when I click on change button the datepicker does not get the focus.

来源:https://stackoverflow.com/questions/3777683/unable-to-set-focus-to-datepicker-using-attached-property-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!