使用组合框更改绑定实例

本文关键字:绑定 实例 组合 | 更新日期: 2023-09-27 18:18:32

如何绑定到类的一个实例?我有一个组合框,如果我改变组合框中的selectedItem,两个输入字段应该用对象实例的属性填充。

下面是我的wpf代码:
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBlock Text="Bearbeiten:" Style="{StaticResource TextBlockHeader}" Grid.Row="0"/>
    <ComboBox ItemsSource="{Binding LinkList}" DisplayMemberPath="DisplayName" Style="{StaticResource defaultComboBox}" Grid.Row="1"/>
    <Separator Style="{StaticResource defaultSeperator}" Grid.Row="2"/>
    <TextBlock Text="DisplayName:" Style="{StaticResource TextBlockHeader}" Grid.Row="3"/>
    <TextBox Name="linkDisplayName" Style="{StaticResource NormalTextBox}" Grid.Row="4"/>
    <TextBlock Text="URL" Style="{StaticResource TextBlockHeader}" Grid.Row="5"/>
    <TextBox Name="linkUrl" Style="{StaticResource LargeTextBox}" Grid.Row="6"/>
</Grid>

我已经在代码隐藏文件(mainWindow.xaml.cs)中设置了DataContext。下面是该文件的代码:

public class mainWindow : MetroWindow
{
    public LinkManager LinkManager { get; set; }
    public mainWindow()
    {
        InitializeComponent();
        this.DataContext = this.LinkManager;
    }
}

LinkManager.cs:

public class LinkManager
{
    public ObservableCollection<Link> LinkList { get; set; }
}

最后是Link.cs

    public class Link
    {
        private string _displayName;
        public string DisplayName
        {
            get { return this._displayName; }
            set { this._displayName = value; }
        }
        private string _url;
        public string Url
        {
            get { return this._url; }
            set { this._url = value; }
        }
    }

ComboBox绑定工作正常:

FirstImage

但是输入字段仍然是空的(这是逻辑…)。我如何用组合框中对象的选定实例的属性填充它们?

SecondImage

有什么建议吗?谢谢!

使用组合框更改绑定实例

如果没有一个好的,最小的完整的代码示例来清楚地说明您的问题,就很难确定最佳解决方案是什么。said…

在我看来,解决您的明显需求的适当方法是以不同的方式设置绑定,这样您就可以直接绑定到SelectedItem属性以更新编辑字段及其绑定。也就是说,不是为了ComboBox.ItemsSource绑定而设置DataContext,而是将DataContext绑定到ComboBox.SelectedItem,然后在编辑字段中使用该上下文进行绑定。

这样,每当用户更改所选项目时,WPF将自动将其与您的需求适当地连接起来。

下面的代码示例说明了我的意思:

XAML:

<Window x:Class="TestSO33231850BindDataContext.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="mainWindow1"
        Title="MainWindow" Height="350" Width="525">
  <!-- Bind container's DataContext to the SelectedItem -->
  <Grid DataContext="{Binding SelectedItem, ElementName=comboBox1}">
    <Grid.Resources>
      <Style TargetType="TextBox">
        <Setter Property="Padding" Value="5, 0"/>
        <Setter Property="Margin" Value="5, 0"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
    </Grid.Resources>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <!-- Bind ItemsSource to source using explicit object
         reference instead of DataContext -->
    <ComboBox x:Name="comboBox1"
              ItemsSource="{Binding DataList, ElementName=mainWindow1}"
              DisplayMemberPath="Name"/>
    <!-- Now, edit fields will automatically track selected item -->
    <TextBlock Text="Name:" TextAlignment="Right" Grid.Row="1"/>
    <TextBlock Text="Value:" TextAlignment="Right" Grid.Row="2"/>
    <TextBox Text="{Binding Name}" Grid.Row="1" Grid.Column="1"/>
    <TextBox Text="{Binding Value}" Grid.Row="2" Grid.Column="1"/>
  </Grid>
</Window>
c#

:

public partial class MainWindow : Window
{
    public List<Data> DataList { get; private set; }
    public MainWindow()
    {
        DataList = new List<Data>();
        DataList.Add(new Data { Name = "Data item 1", Value = "value 1" });
        DataList.Add(new Data { Name = "Data item 2", Value = "value 2" });
        DataList.Add(new Data { Name = "Data item 3", Value = "value 3" });
        InitializeComponent();
    }
}
public class Data
{
    public string Name { get; set; }
    public string Value { get; set; }
}

我只是从头开始写,而不是试图合并你的不完整的例子,因为你的例子缺少太多的东西,让它是实际的,试图完成。我还遗漏了对绑定友好的特性,如ObservableCollection<T>INotifyPropertyChanged实现。我相信即使这样,您也可以从示例中理解我的意思,并且可以将相同的技术适当地应用到您自己的代码中。