我已經張貼了類似的問題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閱讀本
感謝您的快速reply.I'll必須閱讀有關轉換器和的ObservableCollection的東西,因爲我已經做了沒有before.If你有一些資源或約好的教程它請張貼其鏈接.thanks再次 – 2010-06-29 17:56:03
我沒有在我腦海中的任何例子,但現在你可以谷歌「WPF結合」和「的IValueConverter」。基本上,轉換器使UI能夠處理一種數據類型,而您的視圖模型可以與另一種類型一起工作。 問候...... – Padel 2010-06-29 18:01:12
感謝man.thanks – 2010-06-29 18:32:58