C# nsitive=false;}} 公共覆盖验证结果验证(对象值,CultureInfo CultureInfo){ ValidationResult=ValidationResult.ValidResult; if(this.sender.IsReadOnly | | |!this.sender.IsEditable | | |!this.sender.IsEnabled){ 返回结果;} if(!string.IsNullOrEmpty(值为字符串)&&this.itemsSource!=null){ DataRow[]r=this.itemsSource.Select (this.sender.SelectedValuePath+“=”+value.ToString()+“”); if(r.Length==0&&(!this.sender.IsKeyboardFocusWithin | | |!this.firstChance)){ 结果=新的ValidationResult (false,“输入的值不在列表中!”;} 返回结果;} void Invalidate_OnLostFocus(对象发送方,路由目标){ Assert(((SWC.ComboBox)e.Source).Equals(this.sender), “发件人对象无效!”; 这个。无效(假);} ///是的,如果我们想等待LostFocus(我们的组合框), ///如果现在应该检查用户的输入,则为false。 ///如果不应更新firstChance标志,则为null 私有无效失效(bool?firstChance){ 如果((this.sender.IsEditable)&(!this.sender.IsReadOnly)&& (!this.sender.IsKeyboardFocusWithin)&&(!string.IsNullOrEmpty(this.SenderText))){ if(firstChance.HasValue){ this.firstChance=firstChance.Value;} //使用ValidationResult Validate强制验证发件人对象->的有效性 this.sender.GetBindingExpression (ComboBox.TextProperty).UpdateSource();} 私有数据表GetItemsSource (对象项资源){ DataTable=null; if(itemsSource.GetType()==typeof(DataTable)){ table=作为数据表的itemsSource; } if(itemsSource.GetType()==typeof(DataView)){ 表=((数据视图)项资源).ToTable(); } Assert(表!=null, “未知源类型”+itemsSource.GetType().Name); 返回表;} 私有字符串发送文本{ 获取{var text=(this.sender!=null)?this.sender.text:string.Empty; 如果(text==null){text=string.Empty;} 返回text.ToString().Trim();} }
我的XAML代码是:C# nsitive=false;}} 公共覆盖验证结果验证(对象值,CultureInfo CultureInfo){ ValidationResult=ValidationResult.ValidResult; if(this.sender.IsReadOnly | | |!this.sender.IsEditable | | |!this.sender.IsEnabled){ 返回结果;} if(!string.IsNullOrEmpty(值为字符串)&&this.itemsSource!=null){ DataRow[]r=this.itemsSource.Select (this.sender.SelectedValuePath+“=”+value.ToString()+“”); if(r.Length==0&&(!this.sender.IsKeyboardFocusWithin | | |!this.firstChance)){ 结果=新的ValidationResult (false,“输入的值不在列表中!”;} 返回结果;} void Invalidate_OnLostFocus(对象发送方,路由目标){ Assert(((SWC.ComboBox)e.Source).Equals(this.sender), “发件人对象无效!”; 这个。无效(假);} ///是的,如果我们想等待LostFocus(我们的组合框), ///如果现在应该检查用户的输入,则为false。 ///如果不应更新firstChance标志,则为null 私有无效失效(bool?firstChance){ 如果((this.sender.IsEditable)&(!this.sender.IsReadOnly)&& (!this.sender.IsKeyboardFocusWithin)&&(!string.IsNullOrEmpty(this.SenderText))){ if(firstChance.HasValue){ this.firstChance=firstChance.Value;} //使用ValidationResult Validate强制验证发件人对象->的有效性 this.sender.GetBindingExpression (ComboBox.TextProperty).UpdateSource();} 私有数据表GetItemsSource (对象项资源){ DataTable=null; if(itemsSource.GetType()==typeof(DataTable)){ table=作为数据表的itemsSource; } if(itemsSource.GetType()==typeof(DataView)){ 表=((数据视图)项资源).ToTable(); } Assert(表!=null, “未知源类型”+itemsSource.GetType().Name); 返回表;} 私有字符串发送文本{ 获取{var text=(this.sender!=null)?this.sender.text:string.Empty; 如果(text==null){text=string.Empty;} 返回text.ToString().Trim();} },c#,wpf,combobox,itemssource,validationrule,C#,Wpf,Combobox,Itemssource,Validationrule,我的XAML代码是: <ComboBox Name="bMS_ComboBox" ItemsSource='{Binding Path="[BMS,ANR]"}' Text="{Binding [BMS]}" SelectedValuePath="F1" DisplayMemberPath="F1" Style="{StaticResource LocalComboBoxStyle}" IsEditable="True" c
<ComboBox
Name="bMS_ComboBox"
ItemsSource='{Binding Path="[BMS,ANR]"}'
Text="{Binding [BMS]}"
SelectedValuePath="F1"
DisplayMemberPath="F1"
Style="{StaticResource LocalComboBoxStyle}"
IsEditable="True"
c:Field.Restricted="True">
c是我的类字段的命名空间,AttachedProperty受限:
xmlns:c=“clr命名空间:MyAssembly.Controls;assembly=MyAssembly.Controls”
在我的样式文件中,我有以下代码:
<Style x:Key="LocalComboBoxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images/exclamation_diamond.png"
Height="16"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Margin="0 0 2 0"/>
<Border BorderBrush="Red" BorderThickness="1" Opacity="0.6">
<Border.BitmapEffect>
<BlurBitmapEffect Radius="6"/>
</Border.BitmapEffect>
<AdornedElementPlaceholder/>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
看起来像WPF和C,但您的问题或标签中没有提到这一点。想要回答C#或WPF特定问题的人不会那么容易找到你的问题。你是对的!但是我在哪里可以知道呢?我编辑了你的问题。我在你的帖子中添加了C#和WPF标记,在你的问题中,第一句话说现在你正在使用C#和XAML。太棒了!非常感谢:-)看起来像WPF和C,但您的问题或标签中没有提到。想要回答C#或WPF特定问题的人不会那么容易找到你的问题。你是对的!但是我在哪里可以知道呢?我编辑了你的问题。我在你的帖子中添加了C#和WPF标记,在你的问题中,第一句话说现在你正在使用C#和XAML。太棒了!非常感谢:-)谢谢,看起来不错!不幸的是,它在我的应用程序中不起作用:-(如果不复杂,请解释:为什么?我不知道。我想,DataContext可能有问题。但也可能是其他问题。我正在研究另一种解决方案。如果有,我可以在这里介绍。谢谢,它看起来不错!不幸的是,它在我的应用程序中不起作用:-(如果不是为了复杂解释:为什么?我不知道。我想,DataContext可能有问题。但也可能是其他问题。我正在研究另一种解决方案。如果有,我可以在这里介绍。
private static void OnChangedRestricted(DependencyObject o, DependencyPropertyChangedEventArgs e) {
Control c = o as Control;
if (c != null && (bool)e.NewValue == true) {
if (c.GetType() == typeof(ComboBox)) {
((ComboBox)c).Loaded += new RoutedEventHandler(RestrictedComboBox_Loaded);
static void RestrictedComboBox_Loaded
(object sender, RoutedEventArgs e) {
ComboBox cb = (ComboBox)sender;
BindingExpression be = cb.GetBindingExpression
(ComboBox.TextProperty);
if (be != null) {
Binding b = be.ParentBinding;
b.ValidationRules.Add
(new RestrictedComboBoxItemValidationRule
((ComboBox)sender)); } }
public class RestrictedComboBoxItemValidationRule : ValidationRule {
private DataTable itemsSource;
private ComboBox sender;
private bool firstChance;
public RestrictedComboBoxItemValidationRule(ComboBox sender) {
this.sender = sender;
this.firstChance = true;
this.sender.LostFocus += new RoutedEventHandler(Invalidate_OnLostFocus);
this.itemsSource = this.GetItemsSource (sender.ItemsSource);
if (this.itemsSource != null) {
this.itemsSource.CaseSensitive = false; } }
public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
ValidationResult result = ValidationResult.ValidResult;
if (this.sender.IsReadOnly || !this.sender.IsEditable || !this.sender.IsEnabled) {
return result; }
if (!string.IsNullOrEmpty(value as string) && this.itemsSource != null) {
DataRow[] r = this.itemsSource.Select
(this.sender.SelectedValuePath + " = '" + value.ToString() + "'");
if (r.Length == 0 && (!this.sender.IsKeyboardFocusWithin || !this.firstChance)) {
result = new ValidationResult
(false, "The entered value is not in list!"); } }
return result; }
void Invalidate_OnLostFocus(object sender, RoutedEventArgs e) {
Debug.Assert (((SWC.ComboBox)e.Source).Equals(this.sender),
"Sender-Object invalide!");
this.Invalidate(false); }
/// <param name="firstChance">true, if we want to wait for LostFocus (for our ComboBox),
/// false, if user's inputs now should be checked.
/// null, if no update of the firstChance-flag should occur</param>
private void Invalidate(bool? firstChance) {
if ((this.sender.IsEditable) && (!this.sender.IsReadOnly) &&
(!this.sender.IsKeyboardFocusWithin) && (!string.IsNullOrEmpty(this.SenderText))) {
if (firstChance.HasValue) {
this.firstChance = firstChance.Value; }
// force validation of sender-Object -> with ValidationResult Validate
this.sender.GetBindingExpression
(ComboBox.TextProperty).UpdateSource(); } }
private DataTable GetItemsSource
(object itemsSource) {
DataTable table = null;
if (itemsSource.GetType() == typeof(DataTable)) {
table = itemsSource as DataTable;
}
if (itemsSource.GetType() == typeof(DataView)) {
table = ((DataView)itemsSource).ToTable();
}
Debug.Assert(table != null,
"Unknown source type " + itemsSource.GetType ().Name);
return table; }
private string SenderText {
get { var text = (this.sender != null) ? this.sender.Text : string.Empty;
if (text == null) { text = string.Empty; }
return text.ToString().Trim(); } }
}
<ComboBox
Name="bMS_ComboBox"
ItemsSource='{Binding Path="[BMS,ANR]"}'
Text="{Binding [BMS]}"
SelectedValuePath="F1"
DisplayMemberPath="F1"
Style="{StaticResource LocalComboBoxStyle}"
IsEditable="True"
c:Field.Restricted="True">
<Style x:Key="LocalComboBoxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images/exclamation_diamond.png"
Height="16"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Margin="0 0 2 0"/>
<Border BorderBrush="Red" BorderThickness="1" Opacity="0.6">
<Border.BitmapEffect>
<BlurBitmapEffect Radius="6"/>
</Border.BitmapEffect>
<AdornedElementPlaceholder/>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>