2010-06-29 101 views
0

我已經張貼了類似的問題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閱讀本

回答

1

它是這樣的:您設置的數據上下文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

相關問題