Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动完成盒验证_C#_Silverlight - Fatal编程技术网

C# 自动完成盒验证

C# 自动完成盒验证,c#,silverlight,C#,Silverlight,我在silverlight中有一个自动完成框,它绑定到一个集合。它工作得很好。我只是想要它,这样用户就不能输入任何不在集合中的值 例如:集合包含一个值“Head”。如果用户输入Headx或其他内容,则应启动验证 如何做到这一点 问候 阿伦试试这个 <Sdk:AutoCompleteBox Grid.Column="3" Grid.Row="3" Height="18" Width="150" IsTextCompletionEnabled="True" TabIndex="9"

我在silverlight中有一个自动完成框,它绑定到一个集合。它工作得很好。我只是想要它,这样用户就不能输入任何不在集合中的值

例如:集合包含一个值“Head”。如果用户输入Headx或其他内容,则应启动验证

如何做到这一点

问候

阿伦

试试这个

<Sdk:AutoCompleteBox Grid.Column="3" Grid.Row="3" Height="18" Width="150" 
     IsTextCompletionEnabled="True" TabIndex="9" HorizontalAlignment="Left"

     Text="{Binding ElementName=ResEdit,Path=DataContext.SelectedDemoText,Mode=TwoWay}"
     ItemsSource="{Binding ElementName=ResEdit,Path=DataContext.DemoList,Mode=OneWay}"
     ItemTemplate="{StaticResource DemoTemplate}"
     ValueMemberPath="DemoCode" 
     LostFocus="AutoCompleteBox_LostFocus"
     Margin="0,0,21,0" Padding="0">
  </Sdk:AutoCompleteBox>

您应该能够更改绑定来实现这一点

默认情况下,文本属性通常在控件失去焦点时更新绑定源。通过将绑定设置为更新PropertyChanged上的源,可以在每次击键时实现验证

您对Text属性的绑定看起来像这样

Text="{Binding MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
在您的属性设置器中,您可以在认为合适的时候抛出一个验证接口,或者您可以实现一个验证接口(or)并以这种方式处理它


我最近一直在做这个。我的问题通过检查解决了 “自动完成”框的“失去焦点”事件上的“选定项”属性

private void autoCompleteBox1_LostFocus(object sender, RoutedEventArgs e)     
{     
     if (autoCompleteBox1.SelectedItem == null && !string.IsNullOrEmpty(autoCompleteBox1.Text))
      {
       MessageBox.Show("Please fill in the right value");
       autoCompleteBox1.Text = "";
       autoCompleteBox1.Focus();
        }
}
问候


Priya

每次SelectedItem属性为Null或不在GotFocus上时,您应该检查一下
private void autoCompleteBox1_LostFocus(object sender, RoutedEventArgs e)     
{     
     if (autoCompleteBox1.SelectedItem == null && !string.IsNullOrEmpty(autoCompleteBox1.Text))
      {
       MessageBox.Show("Please fill in the right value");
       autoCompleteBox1.Text = "";
       autoCompleteBox1.Focus();
        }
}