代码之家  ›  专栏  ›  技术社区  ›  black sensei

WPF组合框数据绑定到自定义对象和datatable.showing System.Data.DataRowView在下拉列表项中

  •  0
  • black sensei  · 技术社区  · 15 年前

    我发布了一个类似的问题 here

    <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.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 这是相反的行为,因为在选定的项目是正确的,下拉列表项目显示 。使用它们都会引发一个异常,因为我无法同时使用它们来正确显示选定项和下拉列表项。

    我真的不知道我做得不对。谁能告诉我一些事情我会非常感激的。谢谢你读这篇文章

    1 回复  |  直到 8 年前
        1
  •  1
  •   Padel    15 年前

    它是这样的:将组合框的数据上下文设置为DataTable类型的实例。然后将ItemsSource设置为{Binding},这意味着组合框中的每个项都将绑定到一个DataRow(它既没有loginname属性,也没有username属性)。在这里绑定停止工作。从DataRow到UserLogin没有隐式的转换方法。

    不管是哪种情况,都要放弃 <ComboBox.Text> ... </ComboBox.Text> 部分。

    希望这对你有帮助。。。