Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# WPF组合框数据绑定到自定义对象和datatable.showing System.Data.DataRowView(显示下拉列表项中的System.Data.DataRowView)_C#_Wpf_Xaml_Combobox_Binding - Fatal编程技术网

C# WPF组合框数据绑定到自定义对象和datatable.showing System.Data.DataRowView(显示下拉列表项中的System.Data.DataRowView)

C# WPF组合框数据绑定到自定义对象和datatable.showing System.Data.DataRowView(显示下拉列表项中的System.Data.DataRowView),c#,wpf,xaml,combobox,binding,C#,Wpf,Xaml,Combobox,Binding,我发布了一个类似的问题,无法成功实现向我建议的解决方案,因为它不起作用。我找到了一种解决方法,希望通过将combobox绑定到自定义对象以启用数据验证来改进它。下面是此解决方案的xaml <Window xmlns:data="clr-namespace:Myproject"> <Window.Resources> <data:UserLogin x:Key="user"></data:UserLogin> <DataTemplat

我发布了一个类似的问题,无法成功实现向我建议的解决方案,因为它不起作用。我找到了一种解决方法,希望通过将combobox绑定到自定义对象以启用数据验证来改进它。下面是此解决方案的xaml

<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>
并且UserLogin类是

 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
但下拉列表项目显示正确,当我将
ItemTemplate
displaymberpath
交换时,与所选项目正确且下拉列表项目显示相反的行为
System.Data.DataRowView
。使用它们都会引发异常,因为我无法同时使用它们来正确显示选定项和下拉列表项


我真的不知道我做得有什么不对。有人能解释一下吗?我将非常感谢。感谢您阅读此

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

您可以实现一个转换器来进行转换,或者逐个将行转换为UserLogin,并将组合框的DataContext设置为UserLogin列表(或者如果需要更高级的功能,可以使用observeCollection)

在任何一种情况下,请删除
零件


希望这对您有所帮助…

感谢您的快速回复。我必须阅读有关转换器和可观察到的收集内容,因为我以前没有做过任何事情。如果您有一些关于它的资源或好教程,请发布其链接。感谢againI,我现在想不出任何示例,但您可以在谷歌上搜索“wpf绑定”和“IValueConverter”。基本上,转换器使UI能够处理一种类型的数据,而视图模型能够处理另一种类型的数据。当做
 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; }
    }
}