如何绑定一个类的实例? 我有一个组合框,如果我在组合框中更改selectedItem,则应该用对象实例的属性填充2个输入字段。
这是我的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绑定运行良好:
但是输入字段仍为空(这是逻辑......)。如何在组合框中填充所选对象实例的属性?
有什么建议吗?谢谢!
答案 0 :(得分:0)
如果没有a good, minimal, complete code example清楚地说明您的问题,很难确定最佳解决方案是什么。那说......
在我看来,解决您明显需要的方法是以不同方式设置绑定,以便您可以直接绑定到SelectedItem
属性以更新编辑字段及其绑定。即而不是为DataContext
绑定的好处设置ComboBox.ItemsSource
,而是将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
实现。我相信即便如此,你可以从示例中看出我的意思,并且可以适当地将相同的技术应用于你自己的代码。