سیدسبحان
سه شنبه 22 دی 1394, 22:55 عصر
سلام
توی الگوی mvvm من میخام به کاربر، اجازه تایپ کردن حروف فارسی و انگلیسی رو بگیرم و فقط عدد رو تایپ کنه.
اینطوری که تحقیق کردم ، متوجه شدم که باید از dependency Property استفاده کنم.
اینارو که دقیقا از روی یه چندتا مثال ایده گرفتم، نوشتم ولی یه خطای عجیب میده که اصلا نمیفهمم منظورش چی هست.
ممنون میشم کمکم کنید.
اینا کد xaml ام هست:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vi="clr-namespace:WpfApplication1.View"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="390" Width="557" Loaded="Window_Loaded"
>
<TextBox
x:Name="Box1"
HorizontalAlignment="Left" Height="24"
Margin="393,24,0,0" TextWrapping="Wrap"
Text="{Binding Path=Name,Mode=TwoWay,
ValidatesOnDataErrors=True, UpdateSourceTrigger=Explicit,TargetNullValue=''}"
Validation.ErrorTemplate="{StaticResource ErrorTemplate}"
VerticalAlignment="Top" Width="136" PreviewTextInput="Box1_PreviewTextInput"
wpfApplication1:AlphabetCharecterClass.AlphabetCha recter="True"
>
</TextBox>
</Window>
اینم کدهای dependency property هست:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication1
{
public class AlphabetCharecterClass
{
public static readonly DependencyProperty AlphabetCharecterProperty = DependencyProperty.Register("AlphabetCharecter", typeof(bool),
typeof(AlphabetCharecterClass),
new PropertyMetadata(true, OnAlphabetCharecterPropertyChanged));
public static void SetAlphabetCharecter(DependencyObject dp, bool value)
{
dp.SetValue(AlphabetCharecterProperty, value);
}
public static bool GetAlphabetCharecter(DependencyObject dp)
{
return (bool)dp.GetValue(AlphabetCharecterProperty);
}
private static void OnAlphabetCharecterPropertyChanged(DependencyObjec t dp,
DependencyPropertyChangedEventArgs e)
{
var txt = dp as TextBox;
if (txt == null)
return;
if ((bool)e.NewValue)
{
txt.PreviewTextInput += HandlePreviewKeyDown;
}
else
{
txt.PreviewTextInput -= HandlePreviewKeyDown;
}
}
static void HandlePreviewKeyDown(object sender, TextCompositionEventArgs e)
{
var txt = sender as TextBox;
if (!char.IsDigit(e.Text, e.Text.Length - 1))
{
e.Handled = true;
}
var binding = txt.GetBindingExpression(TextBox.TextProperty);
if (binding == null) return;
binding.UpdateSource();
}
}
}
اینم خطایی که میده:
'AlphabetCharecterClass' type must derive from DependencyObject
توی الگوی mvvm من میخام به کاربر، اجازه تایپ کردن حروف فارسی و انگلیسی رو بگیرم و فقط عدد رو تایپ کنه.
اینطوری که تحقیق کردم ، متوجه شدم که باید از dependency Property استفاده کنم.
اینارو که دقیقا از روی یه چندتا مثال ایده گرفتم، نوشتم ولی یه خطای عجیب میده که اصلا نمیفهمم منظورش چی هست.
ممنون میشم کمکم کنید.
اینا کد xaml ام هست:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vi="clr-namespace:WpfApplication1.View"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="390" Width="557" Loaded="Window_Loaded"
>
<TextBox
x:Name="Box1"
HorizontalAlignment="Left" Height="24"
Margin="393,24,0,0" TextWrapping="Wrap"
Text="{Binding Path=Name,Mode=TwoWay,
ValidatesOnDataErrors=True, UpdateSourceTrigger=Explicit,TargetNullValue=''}"
Validation.ErrorTemplate="{StaticResource ErrorTemplate}"
VerticalAlignment="Top" Width="136" PreviewTextInput="Box1_PreviewTextInput"
wpfApplication1:AlphabetCharecterClass.AlphabetCha recter="True"
>
</TextBox>
</Window>
اینم کدهای dependency property هست:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication1
{
public class AlphabetCharecterClass
{
public static readonly DependencyProperty AlphabetCharecterProperty = DependencyProperty.Register("AlphabetCharecter", typeof(bool),
typeof(AlphabetCharecterClass),
new PropertyMetadata(true, OnAlphabetCharecterPropertyChanged));
public static void SetAlphabetCharecter(DependencyObject dp, bool value)
{
dp.SetValue(AlphabetCharecterProperty, value);
}
public static bool GetAlphabetCharecter(DependencyObject dp)
{
return (bool)dp.GetValue(AlphabetCharecterProperty);
}
private static void OnAlphabetCharecterPropertyChanged(DependencyObjec t dp,
DependencyPropertyChangedEventArgs e)
{
var txt = dp as TextBox;
if (txt == null)
return;
if ((bool)e.NewValue)
{
txt.PreviewTextInput += HandlePreviewKeyDown;
}
else
{
txt.PreviewTextInput -= HandlePreviewKeyDown;
}
}
static void HandlePreviewKeyDown(object sender, TextCompositionEventArgs e)
{
var txt = sender as TextBox;
if (!char.IsDigit(e.Text, e.Text.Length - 1))
{
e.Handled = true;
}
var binding = txt.GetBindingExpression(TextBox.TextProperty);
if (binding == null) return;
binding.UpdateSource();
}
}
}
اینم خطایی که میده:
'AlphabetCharecterClass' type must derive from DependencyObject