WPF数据绑定组合框既自定义对象,并在下拉列表中的项目datatable.showing System.Data.DataRowView

问题描述:

我已经张贴了类似的问题here,并没有能够成功实施解决方案向我建议,因为它是不working.I've找到一个解决方法,并希望通过组合框绑定到自定义对象,以使数据validation.here改善它是这一个WPF数据绑定组合框既自定义对象,并在下拉列表中的项目datatable.showing System.Data.DataRowView

<Window xmlns:data="clr-namespace:Myproject"> 

<Window.Resources> 
    <data:UserLogin x:Key="user"></data:UserLogin> 
    <DataTemplate x:Key="comboTemplate"> 
     <TextBlock Text="{Binding Path=username}" /> 
    </DataTemplate> 
</Window.Resources> 
<ComboBox Margin="18,121,24,0" Name="cmbEmail" Tag="email" TabIndex="1" ToolTip="enter the email you signed up with here" IsEditable="True" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource comboTemplate}" ItemsSource="{Binding}" Height="23" VerticalAlignment="Top" Style="{DynamicResource cmbBoxerrors}"> 
      <ComboBox.Text> 
       <Binding Path="Loginname" Source="{StaticResource user}" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"> 
        <Binding.ValidationRules> 
         <ExceptionValidationRule/> 
        </Binding.ValidationRules> 
       </Binding> 
      </ComboBox.Text>  
</ComboBox> 
</Window> 

的XAML和xaml.cs是

  if (con != null) 
      { 
       if (con.State == ConnectionState.Closed) 
        con.Open(); 

       SqlCeCommand cmdusers = new SqlCeCommand("select * from users order by id", con); 

       SqlCeDataAdapter da = new SqlCeDataAdapter(cmdusers); 
       userdt = new DataTable("users"); 
       da.Fill(userdt); 

       cmbEmail.DataContext = userdt; 

      } 

和用户登陆类是

class UserLogin :IDataErrorInfo 
{ 
    private string _loginname = ""; 
    private string _password; 


    public string this[string columnName] 
    { 
     get 
     { 


      string result = null; 
      if(columnName == "Loginname") 
      { 
       if(string.IsNullOrEmpty(this._loginname)) 
       { 
        result = "Login Name cannot be Empty"; 
       } 
      } 

      if (columnName == "Loginname") 
      { 
       if(!Util.ValidateRegexPatern(Properties.Resources.emailRegex,this._loginname)) 
       { 
        result = "MalFormed Email address. Please write a correct email addess"; 
       } 
      } 

      return result; 
     } 
    } 

    public string Error 
    { 
     get { return null; } 
    } 

    public string Password 
    { 
     get { return _password; } 
     set { _password = value; } 
    } 

    public string Loginname 
    { 
     get { return _loginname; } 
     set { _loginname = value; } 
    } 
} 

的问题是,当我使用ItemTemplate所选项目显示System.Data.DataRowView但下拉列表项正确显示,当我与DisplayMemberPath交换ItemTemplate它是相反的行为,如选择的项目是正确的下拉列表项显示System.Data.DataRowView。使用它们都会抛出异常,因为我无法使用它们,而且所选的下拉列表项都正确显示。

我真的不知道我没有做任何correctly.Can揭示出这个一些轻我会柠thankfull.Thanks阅读本

它是这样的:您设置的数据上下文ComboBox的类型为DataTable类型的实例。然后设置的ItemsSource到{结合},这意味着在ComboBox每个项目将结合一个DataRow(其不具有任一登录名,也不用户名作为属性)。这里绑定停止工作。没有从DataRow转换到用户登录的隐式方法。

您可以实现一个转换器来进行转换,或将行转换为UserLogin并将ComboBox的DataContext设置为UserLogin的列表(或者如果需要更高级的功能,则为ObservableCollection)。

在任一情况下丢弃该<ComboBox.Text> ... </ComboBox.Text>一部分。

希望这有助于你...

+0

感谢您的快速reply.I'll必须阅读有关转换器和的ObservableCollection的东西,因为我已经做了没有before.If你有一些资源或约好的教程它请张贴其链接.thanks再次 – 2010-06-29 17:56:03

+0

我没有在我脑海中的任何例子,但现在你可以谷歌“WPF结合”和“的IValueConverter”。基本上,转换器使UI能够处理一种数据类型,而您的视图模型可以与另一种类型一起工作。 问候...... – Padel 2010-06-29 18:01:12

+0

感谢man.thanks – 2010-06-29 18:32:58