我有一个自定义日期选择器,我正在尝试使用附加属性设置焦点 . 我有在2010年2月发布的工具包版本3.5.50211.1 . 当我在我的代码中执行 DatePicker.Focus(); 时,焦点工作正常 . 我的自定义datepicker代码如下:

  • 我的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
    }
}

我在我的代码中使用此datepicker并尝试使用上面定义的Isfocus附加属性设置焦点 .

  • 测试形式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>
  • 测试表格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;
        }
    }
}

现在,当我点击更改按钮时,datepicker无法获得焦点 .