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