无法使用 wpf 中的附加属性将焦点设置为日期选择器

发布于 2024-09-24 14:32:51 字数 5298 浏览 0 评论 0原文

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

-- 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
    }
}

我在代码中使用此日期选择器,并尝试使用上面定义的 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;
        }
    }
}

现在,当我单击更改按钮时,日期选择器不会获得焦点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文