1
我一直在解决这个问题一段时间,所以今天我开始了一个新项目,并简化了基本的东西。我想要做的是有一个列表框,绑定到一个集合,并具有下面显示的选定项目的详细视图。除了引用其他表的项目只显示外键之外,它一切正常。我能够用linq查询来解决这个问题,但意识到我使用的方法是骇人听闻的。如何处理WPF DataBinding中的Linq到SQL外键关系
public partial class Window1 : Window
{
DataClasses1DataContext db = new DataClasses1DataContext();
public IEnumerable<Issue> issues = null;
public Window1()
{
InitializeComponent();
issues = from i in db.Issues
select i;
this.DataContext = issues;
}
// The hacked in query that correctly displays the name instead of Key
private void IssueListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<Issue> _issues = issues.ToList();
int empl = _issues[IssueListBox.SelectedIndex].IssOwner;
OwnerTextBlock.Text = (from emp in db.Employees
where emp.EmpID == empl
select emp.EmpFirstName.ToString() + " " +
emp.EmpLastName.ToString()).Single();
}
}
XAML中那样简单,因为我可以得到它:
<Window.Resources>
<DataTemplate x:Key="ShowIssueDetail">
<TextBlock Text="{Binding Path=IssTitle}" FontWeight="Bold" FontSize="14"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock Text="Issues" FontWeight="Bold" />
<ListBox x:Name="IssueListBox"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ShowIssueDetail}"
IsSynchronizedWithCurrentItem="True"
HorizontalContentAlignment="Stretch" SelectionChanged="IssueListBox_SelectionChanged">
</ListBox>
<TextBlock Text="{Binding Path=IssDescription}" />
<TextBlock Text="{Binding Path=IssOwner}" /> <!--I want this to show the name, not the Key-->
<TextBlock Name='OwnerTextBlock' /> <!--This has the name set in code from the Linq query and works-->
</StackPanel>
如何正确地做到这一点?
你不知道我有多少次Google和Bing试图获取相关记录,我已经在WinForms中使用过它们,但是找不到绑定到它们的方法。我不得不将Employee.FirstName更改为Employee.EmpFirstName,但是这个技巧。 我已经完成了IValueConverters我应该没有与IMultiValueConverter问题。 感谢 – 2010-01-12 01:30:26