У меня есть пользовательский указатель даты, и я пытаюсь установить фокус на это, используя прикрепленное свойство.У меня есть версия инструментария 3.5.50211.1, выпущенная в феврале 2010 года. Фокус работает нормально, когда я делаю 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;
}
}
}
Теперь, когда я нажимаю кнопку изменения, средство выбора даты не получает фокус.