soInput.xaml摘要:
xmlns:core="clr-namespace:MyProject.Fun.Core"
xmlns:behave="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<TextBox Name="txtValue"
Margin="0"
Style="{DynamicResource soTextBox}"
TextWrapping="NoWrap"
Height="50"
Foreground="#FF363636"
FontSize="18.667"
FontWeight="Bold"
Grid.ColumnSpan="2"
IsEnabled="{Binding IsEnabled,ElementName=Input}"
Text="{Binding soValue,ElementName=Input,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
GotFocus="txtValue_GotFocus"
LostFocus="txtValue_LostFocus">
<behave:Interaction.Behaviors>
<core:TextBoxKeyboardBehavior/>
</behave:Interaction.Behaviors>
</TextBox>
soInput.xaml.cs:
namespace MyProject.Fun
{
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
public partial class soInput : UserControl, IComponentConnector
{
public static readonly DependencyProperty soErrorProperty = DependencyProperty.Register("soError", typeof(string), typeof(soInput));
public static readonly DependencyProperty soHelpDescriptionProperty = DependencyProperty.Register("soHelpDescription", typeof(string), typeof(soInput), new UIPropertyMetadata(""));
public static readonly DependencyProperty soHelpTitleProperty = DependencyProperty.Register("soHelpTitle", typeof(string), typeof(soInput), new UIPropertyMetadata(""));
public static readonly DependencyProperty soLabelProperty = DependencyProperty.Register("soLabel", typeof(string), typeof(soInput));
public static readonly DependencyProperty soValueProperty = DependencyProperty.Register("soValue", typeof(string), typeof(soInput));
public soInput()
{
this.InitializeComponent();
}
private void Input_GotFocus(object sender, RoutedEventArgs e)
{
}
private void txtValue_GotFocus(object sender, RoutedEventArgs e)
{
}
private void txtValue_LostFocus(object sender, RoutedEventArgs e)
{
}
public string soError
{
get
{
return (string) base.GetValue(soErrorProperty);
}
set
{
base.SetValue(soErrorProperty, value);
}
}
public string soHelpDescription
{
get
{
return (string) base.GetValue(soHelpDescriptionProperty);
}
set
{
base.SetValue(soHelpDescriptionProperty, value);
}
}
public string soHelpTitle
{
get
{
return (string) base.GetValue(soHelpTitleProperty);
}
set
{
base.SetValue(soHelpTitleProperty, value);
}
}
public string soLabel
{
get
{
return (string) base.GetValue(soLabelProperty);
}
set
{
base.SetValue(soLabelProperty, value);
}
}
public string soValue
{
get
{
return (string) base.GetValue(soValueProperty);
}
set
{
base.SetValue(soValueProperty, value);
}
}
}
}
TextBoxKeyboardBehavior.cs:
using MyProject.Fun.Helpers;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
namespace MyProject.Fun.Core
{
public partial class TextBoxKeyboardBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
base.AssociatedObject.GotFocus += new RoutedEventHandler(this.AssociatedObject_GotFocus);
base.AssociatedObject.LostFocus += new RoutedEventHandler(this.AssociatedObject_LostFocus);
}
private void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
{
Button button = base.AssociatedObject.Template.FindName("keyboardBtn", base.AssociatedObject) as Button;
if (button != null)
{
button.Click -= new RoutedEventHandler(this.btn_Click);
}
if (button != null)
{
button.Visibility = Visibility.Hidden;
}
}
private void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
{
Button button = base.AssociatedObject.Template.FindName("keyboardBtn", base.AssociatedObject) as Button;
if (button == null)
{
base.AssociatedObject.ApplyTemplate();
}
button = (base.AssociatedObject.Template.FindName("keyboardBtn", base.AssociatedObject) as Button);
if (button != null)
{
button.Click += new RoutedEventHandler(this.btn_Click);
button.Visibility = Visibility.Visible;
}
}
private void btn_Click(object sender, RoutedEventArgs e)
{
BindingExpression bindingExpression = BindingOperations.GetBindingExpression(base.AssociatedObject, TextBox.TextProperty);
Keyboard keyboard = new Keyboard();
Binding binding = new Binding();
binding.Source = bindingExpression.DataItem;
binding.Path = bindingExpression.ParentBinding.Path;
binding.Mode = bindingExpression.ParentBinding.Mode;
binding.UpdateSourceTrigger = bindingExpression.ParentBinding.UpdateSourceTrigger;
binding.NotifyOnTargetUpdated = bindingExpression.ParentBinding.NotifyOnTargetUpdated;
binding.NotifyOnSourceUpdated = bindingExpression.ParentBinding.NotifyOnSourceUpdated;
binding.NotifyOnValidationError = bindingExpression.ParentBinding.NotifyOnValidationError;
binding.ValidatesOnDataErrors = bindingExpression.ParentBinding.ValidatesOnDataErrors;
keyboard.TextTxt.SetBinding(TextBox.TextProperty, binding);
bool visibility = NavigationHelper.MainLayout.ModalBackground.Visibility != Visibility.Visible;
Util.ShowKeyboardForm(keyboard, visibility);
}
private object GetValue(string path, object dataItem)
{
string[] array = path.Split(new char[]
{
'.'
});
object obj = null;
System.Reflection.PropertyInfo property = dataItem.GetType().GetProperty(path);
object result;
if (property != null)
{
obj = property.GetValue(dataItem, null);
result = obj;
}
else
{
string[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
string name = array2[i];
if (obj == null)
{
property = dataItem.GetType().GetProperty(name);
obj = property.GetValue(dataItem, null);
}
else
{
property = obj.GetType().GetProperty(name);
obj = property.GetValue(obj, null);
}
}
result = obj;
}
return result;
}
}
}
错误: 名称“TextBoxKeyboardBehavior”在命名空间“clr-namespace:MyProject.Fun.Core”中不存在。 在项目的备份它确实工作,但在这种情况下不起作用! 昨晚所有代码都在同一个项目和工作中,但今天不起作用!