Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
将数据绑定到组合框WPF_Wpf - Fatal编程技术网

将数据绑定到组合框WPF

将数据绑定到组合框WPF,wpf,Wpf,我是WPF的新手,需要帮助将数据绑定到组合框中。xaml文件包含下面的标记 <UserControl x:Class="SKAT.Postfordeler.Client.UI.View.ChooseInboxView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml

我是WPF的新手,需要帮助将数据绑定到组合框中。xaml文件包含下面的标记

<UserControl x:Class="SKAT.Postfordeler.Client.UI.View.ChooseInboxView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="42" d:DesignWidth="598">


<Grid>
    <StackPanel Orientation="Horizontal">
        <ComboBox Name="_currentInbox" Width="180"  Margin="5" Height="22" DataContext="{Binding}"  />
        <Label Content="Et job kører allerede i denne indbakke (1500 ud af 1700 poster behandlet)" Name="_currentProcess"  Margin="5" Height="25" />
    </StackPanel>

</Grid>
在视图代码中,我创建了一个方法来绑定包含列表类型的数据

public void FillInboxes(List<Inbox> inboxes)
{
   DataContext = inboxes;
}
公共空白填充框(列出收件箱)
{
DataContext=收件箱;
}

但它不起作用,请提供任何帮助?

我假设您的
收件箱
类由两个属性组成(为简单起见),但可能有任意数量的属性:

public class Inbox
{
    public int ID { get; set; }
    public string Text { get; set; }
}
您可以编写一个
数据模板
,例如:

<Grid.Resources>
    <DataTemplate x:Key="InboxTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Path=ID}"/>
            <TextBlock>:</TextBlock>
            <TextBlock Text="{Binding Path=Text}"/>
        </WrapPanel>
    </DataTemplate>
</Grid.Resources>
编辑:由于您要求一个更简单的解决方案,您只需重写您的
收件箱
类的
ToString()
方法:

protected override string ToString()
{
    return ID.ToString() + ":" + Text;
}

我假设您的
Inbox
类由两个属性组成(为简单起见),但它们可能有任意数量:

public class Inbox
{
    public int ID { get; set; }
    public string Text { get; set; }
}
您可以编写一个
数据模板
,例如:

<Grid.Resources>
    <DataTemplate x:Key="InboxTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Path=ID}"/>
            <TextBlock>:</TextBlock>
            <TextBlock Text="{Binding Path=Text}"/>
        </WrapPanel>
    </DataTemplate>
</Grid.Resources>
编辑:由于您要求一个更简单的解决方案,您只需重写您的
收件箱
类的
ToString()
方法:

protected override string ToString()
{
    return ID.ToString() + ":" + Text;
}

您应该使用
ItemsSource={Binding}
而不是
DataContext={Binding}

 <ComboBox Name="_currentInbox"
      SelectedItem="Hoved"
      Width="180"
      Margin="5"
      Height="22"
      DisplayMemberPath="Name"
      ItemSource="{Binding}" /> 
可视化树中任何框架元素的数据上下文默认为
{Binding}

 <ComboBox Name="_currentInbox"
      SelectedItem="Hoved"
      Width="180"
      Margin="5"
      Height="22"
      DisplayMemberPath="Name"
      ItemSource="{Binding}" /> 


另外,为了使组合框正确显示项目的文本,我想您也需要
DisplayMemberPath
。我假设您需要显示的
Inbox
类的属性是
Name
。请替换为您的相关属性名。

而不是
DataContext={Binding}
您应该拥有
ItemsSource={Binding}

 <ComboBox Name="_currentInbox"
      SelectedItem="Hoved"
      Width="180"
      Margin="5"
      Height="22"
      DisplayMemberPath="Name"
      ItemSource="{Binding}" /> 
可视化树中任何框架元素的数据上下文默认为
{Binding}

 <ComboBox Name="_currentInbox"
      SelectedItem="Hoved"
      Width="180"
      Margin="5"
      Height="22"
      DisplayMemberPath="Name"
      ItemSource="{Binding}" /> 


另外,为了使组合框正确显示项目的文本,我想您也需要
DisplayMemberPath
。我假设您需要显示的
Inbox
类的属性是
Name
。请替换为您的相关属性名。

如果您的Inbox类是

public class Inbox
{
    public int ID { get; set; }
    public string Text { get; set; }
}
如果您不想更改xmal,代码隐藏方法应该是这样的

public void FillInboxes(List<Inbox> inboxes) 
    {
        _currentInbox.DisplayMemberPath = "Text"; // To display the 'Text' property in the combobox dropdown
        //_currentInbox.DisplayMemberPath = "ID"; // To display the 'ID' property in the combobox dropdown
        _currentInbox.DataContext = inboxes; 
    }
公共空白填充框(列出收件箱)
{
_currentInbox.DisplayMemberPath=“Text”;//在组合框下拉列表中显示“Text”属性
//_currentInbox.DisplayMemberPath=“ID”;//在组合框下拉列表中显示“ID”属性
_currentInbox.DataContext=收件箱;
}

如果您的Inbox类

public class Inbox
{
    public int ID { get; set; }
    public string Text { get; set; }
}
如果您不想更改xmal,代码隐藏方法应该是这样的

public void FillInboxes(List<Inbox> inboxes) 
    {
        _currentInbox.DisplayMemberPath = "Text"; // To display the 'Text' property in the combobox dropdown
        //_currentInbox.DisplayMemberPath = "ID"; // To display the 'ID' property in the combobox dropdown
        _currentInbox.DataContext = inboxes; 
    }
公共空白填充框(列出收件箱)
{
_currentInbox.DisplayMemberPath=“Text”;//在组合框下拉列表中显示“Text”属性
//_currentInbox.DisplayMemberPath=“ID”;//在组合框下拉列表中显示“ID”属性
_currentInbox.DataContext=收件箱;
}


收件箱是如何实现的?另外,请尝试绑定组合框的
ItemsSource
,而不是
DataContext
。Inbox被实现为包含属性的类。Inbox是如何实现的?另外,尝试绑定组合框的
ItemsSource
,而不是
DataContext
。Inbox被实现为一个包含属性的类。我对此进行了测试,但不知道为什么不起作用。我粘贴了整个xaml代码供您参考。数据上下文已经有一个值,因此这有点奇怪。在这种情况下,您可以在将DataContext分配给combobox@Debasis-还是不行,我不知道这是怎么回事,我创建了一个简单的实现和它的工作,但当我试图将它应用到我的项目中时,它没有工作。。。Wahhh然后做一件事,只需从xaml和代码隐藏中删除DataContext=“{Binding}”,而不是_currentInbox.DataContext=Inbox,尝试_currentInbox.ItemSource=Inbox您的服务不会返回空列表。。!!我对此进行了测试,但不知道为什么不起作用。我粘贴了整个xaml代码供您参考。数据上下文已经有一个值,因此这有点奇怪。在这种情况下,您可以在将DataContext分配给combobox@Debasis-还是不行,我不知道这是怎么回事,我创建了一个简单的实现和它的工作,但当我试图将它应用到我的项目中时,它没有工作。。。Wahhh然后做一件事,只需从xaml和代码隐藏中删除DataContext=“{Binding}”,而不是_currentInbox.DataContext=Inbox,尝试_currentInbox.ItemSource=Inbox您的服务不会返回空列表。。!!我对此进行了测试,但不知道为什么不起作用。我粘贴了整个xaml代码供您参考。数据上下文已经有一个值,因此这上面有一些奇怪的东西。请检查您的
输出
窗口。它一定显示了一些绑定错误。我测试了这个,但我不知道为什么它不工作。我粘贴了整个xaml代码供您参考。数据上下文已经有一个值,因此这上面有一些奇怪的东西。请检查您的
输出
窗口。它一定显示了一些绑定错误。我不想在我的xaml文件中添加一些额外的代码,只是一个简单的代码。:),这也不起作用……@user335160,我已经测试了
DataTemplate
方法,它对我有效。你可能想展示一下你做了什么,这样我就能看到问题了。查看我文章的编辑版本,我添加了一个更简单的解决方案。非常好。非常好的示例和解释。我不想在我的xaml文件中添加一些额外的代码,只是一个简单的代码。:),这也不起作用……@user335160,我已经测试了
DataTemplate
方法,它对我有效。你可能想展示一下你做了什么,这样我就能看到问题了。查看我文章的编辑版本,我添加了一个更简单的解决方案。非常好。E