WPF 列表框绑定

发布于 2025-01-07 15:11:31 字数 947 浏览 1 评论 0原文

我有一个医生对象,其属性之一是诊所的 ObservableList。它被用在一个窗口中来显示医生的详细信息。我可以获取要绑定到 TextBoxComboBox 控件的各个属性,但无法获取要绑定到 ListBox 的诊所列表。

这是我的 ListBox 的 xaml:

<ListBox Height="318" 
 HorizontalAlignment="Left" 
 Margin="422,0,0,0" 
 Name="lbClinic" 
 VerticalAlignment="Top" 
 Width="158" 
 SelectedValue="{Binding ClinicID, Path=Clinics, Mode=TwoWay, 
                          UpdateSourceTrigger = PropertyChanged}"
 SelectedValuePath="ClinicID" 
 DisplayMemberPath="Name"
 ItemsSource="{Binding DataContext.ClinicList, 
                          ElementName = PhysicianInfoLookup, Mode = OneWay}" 
 SelectionMode="Multiple" />

列表框正确填充了 ClinicList 中的项目,ClinicList 是所有可能的诊所的列表。但是,我无法从要绑定的医生对象获取诊所列表,以便在列表框中选择它的项目。我还想走另一种方式,如果取消选择某个项目,医生对象中的 ObservableList 将相应更改。

如何将医生对象中的诊所 ObservableList 双向绑定到列表框中的诊所列表(诊所对象的 ObservableList)?

谢谢。

I have a physician object, and one of its properties is an ObservableList of clinics. It is being used in a window to show the details of a physician. I can get individual properties to bind to TextBox and ComboBox controls, but I can't get the list of clinics to bind to my ListBox.

Here is the xaml for my ListBox:

<ListBox Height="318" 
 HorizontalAlignment="Left" 
 Margin="422,0,0,0" 
 Name="lbClinic" 
 VerticalAlignment="Top" 
 Width="158" 
 SelectedValue="{Binding ClinicID, Path=Clinics, Mode=TwoWay, 
                          UpdateSourceTrigger = PropertyChanged}"
 SelectedValuePath="ClinicID" 
 DisplayMemberPath="Name"
 ItemsSource="{Binding DataContext.ClinicList, 
                          ElementName = PhysicianInfoLookup, Mode = OneWay}" 
 SelectionMode="Multiple" />

The Listbox populates properly with items from the ClinicList which is a list of all possible clinics. However, I cannot get the Clinics list from the physician object to bind so that it's items are selected in the Listbox. I also want to go the other way and if an item is deselected, the ObservableList in the physician object will change accordingly.

How do I two-way bind the ObservableList of Clinics in my physician object to the list of Clinics (ObservableList of clinic objects) in my Listbox?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2025-01-14 15:11:31

迈克,你的绑定几乎没有问题。
这是一个完整的示例,演示了执行(我认为)您所追求的操作的一种方法。

视图:

<Page.Resources>
    <ViewModel:Physician x:Key="physician"/>
</Page.Resources>
<StackPanel DataContext="{StaticResource physician}" >
    <TextBlock Text="{Binding Name}" Background="Orange"/>
    <TextBlock Text="Works in:"/>
    <ListBox ItemsSource="{Binding Clinics}" 
             SelectedValue="{Binding SelectedClinicId}" 
             SelectedValuePath="Id" DisplayMemberPath="Name" />
</StackPanel>

视图模型:

public class Physician
{
    private int _selectedClinicId;

    public Physician()
    {
        Name = "Overpaid consultant";
        Clinics = new ObservableCollection<Clinic>
                      {
                          new Clinic {Id = 0, Name = "Out Patients"},
                          new Clinic {Id = 1, Name = "ENT"},
                          new Clinic {Id = 2, Name = "GE"},
                      };
    }

    public string Name { get; set; }
    public IEnumerable<Clinic> Clinics { get; private set; }

    public int SelectedClinicId
    {
        get { return _selectedClinicId; }
        set
        {
            if (value != _selectedClinicId)
            {
                Debug.WriteLine(string.Format("setting clinic to: {0}",value));
                _selectedClinicId = value;
            }
        }
    }
}

public class Clinic
{
    public int Id { get; set; }
    public string Name { get; set; }
}

请注意,对于读/写属性,您可能希望引发属性更改通知。

Mike, there are few problems with your bindings.
Here's a complete sample demonstrating one way of doing what (I think) you're after.

View:

<Page.Resources>
    <ViewModel:Physician x:Key="physician"/>
</Page.Resources>
<StackPanel DataContext="{StaticResource physician}" >
    <TextBlock Text="{Binding Name}" Background="Orange"/>
    <TextBlock Text="Works in:"/>
    <ListBox ItemsSource="{Binding Clinics}" 
             SelectedValue="{Binding SelectedClinicId}" 
             SelectedValuePath="Id" DisplayMemberPath="Name" />
</StackPanel>

View model:

public class Physician
{
    private int _selectedClinicId;

    public Physician()
    {
        Name = "Overpaid consultant";
        Clinics = new ObservableCollection<Clinic>
                      {
                          new Clinic {Id = 0, Name = "Out Patients"},
                          new Clinic {Id = 1, Name = "ENT"},
                          new Clinic {Id = 2, Name = "GE"},
                      };
    }

    public string Name { get; set; }
    public IEnumerable<Clinic> Clinics { get; private set; }

    public int SelectedClinicId
    {
        get { return _selectedClinicId; }
        set
        {
            if (value != _selectedClinicId)
            {
                Debug.WriteLine(string.Format("setting clinic to: {0}",value));
                _selectedClinicId = value;
            }
        }
    }
}

public class Clinic
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Note that for read/write properties you would probably want to raise property change notifications.

久随 2025-01-14 15:11:31

您的问题是 SelectedValue。在 ListBox 级别,不支持绑定到多个选择对象。使用绑定执行此操作的唯一真正方法是重新设计您的 ViewModel,以便从绑定返回的诊所列表代表所有诊所,并且其中的每个对象都应该有一个 IsSelected (或类似的)财产。

然后,您可以通过在 ListBox 节点中添加此 XAML,使用样式来处理多重选择:

<ListBox.ItemContainerStyle>
   <Style TargetType="{x:Type ListBoxItem}">
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
   </Style>
</ListBox.ItemContainerStyle>

Your issue is SelectedValue. At the ListBox level, binding to multiple selection objects is not supported. The only real way to do this with bindings would be to rework your ViewModel so that the list of clinics returned from the binding represents all clinics, and each object there should have an IsSelected (or something similar) property.

You can then use a style to handle the multi selection by adding this XAML within your ListBox node:

<ListBox.ItemContainerStyle>
   <Style TargetType="{x:Type ListBoxItem}">
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
   </Style>
</ListBox.ItemContainerStyle>
So尛奶瓶 2025-01-14 15:11:31

您将需要使用一个模板,其中 TextBox 作为名称,ListBox 作为诊所,并且只需将内部 ListBox 路径绑定到诊所即可。 DisplayMemberPath 是单个TextBox 的快捷方式。如果您想要更多,那么您需要单独的控制。

You are going to need to use a template with TextBox for name and ListBox for Clinics and you just bind the internal ListBox path to Clinics. DisplayMemberPath is a short cut a single TextBox. If you want more then you need individual controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文