2
我有一個wpf應用程序,並將datacontext設置爲viewmodel類的實例。這一切都正常工作,除非我需要訪問列表框中的視圖模型的屬性,並將datacontext設置爲包含在ViewModel類中的集合。在MSDN上它說,你可以使用\字符逃跑,但還沒有在window1.xaml.cs如何訪問xaml集合中的基本datacontext
//instantiate the view model
StatusBoardViewModel statusBoardViewModel = new StatusBoardViewModel();
public Window1()
{
InitializeComponent();
// setting the datacontext
this.DataContext = statusBoardViewModel;
}
而XAML中工作對我來說
我的代碼
public class StatusBoardViewModel : INotifyPropertyChanged
{
OIConsoleDataContext db = new OIConsoleDataContext();
// the collection
private IQueryable<Issue> issues;
public IQueryable<Issue> Issues
{
get
{
// Lazy load issues if they have not been instantiated yet
if (issues == null)
QueryIssues(); // This just runs a linq query to set the property
return issues;
}
set
{
if (issues != value)
{
issues = value;
OnPropertyChanged("Issues");
}
}
}
// The property I need to access
private bool showDetailListItems = true;
public bool ShowDetailListItems
{
get
{
return showDetailListItems;
}
set
{
if (showDetailListItems != value)
{
showDetailListItems = value;
OnPropertyChanged("ShowDetailListItems");
}
}
}
}
// This is in the Window1.xaml file
<ListBox x:Name="IssueListBox"
ItemsSource="{Binding Issues}" // Binds the listbox to the collection in the ViewModel
ItemTemplate="{StaticResource ShowIssueDetail}"
IsSynchronizedWithCurrentItem="True"
HorizontalContentAlignment="Stretch" BorderThickness="3"
DockPanel.Dock="Top" VerticalContentAlignment="Stretch"
Margin="2" MinHeight="50" />
// The datatemplate from the app.xaml file
<DataTemplate x:Key="ShowIssueDetail">
<Border CornerRadius="3" Margin="2" MinWidth="400" BorderThickness="2"
BorderBrush="{Binding Path=IssUrgency, Converter={StaticResource IntToRYGBBoarderBrushConverter}}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=IssSubject}" Margin="3" FontWeight="Bold" FontSize="14"/>
<!--DataTrigger will collapse following panel for simple view-->
<StackPanel Name="IssueDetailPanel" Visibility="Visible" Margin="3">
<StackPanel Width="Auto" Orientation="Horizontal">
<TextBlock Text="Due: " FontWeight="Bold"/>
<TextBlock Text="{Binding Path=IssDueDate}" FontStyle="Italic" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="Auto" Orientation="Horizontal">
<TextBlock Text="Category: " FontWeight="Bold"/>
<TextBlock Text="{Binding Path=IssCategory}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
// This is where I have the issue, ShowDetailListItems is in the base class, not the collection
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=ShowDetailListItems, Mode=TwoWay}" Value="False">
<Setter TargetName="IssueDetailPanel" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
我已經學會了TON這樣做,但這個當前問題是驅動我batty,沒有運氣與谷歌,MSDN,SO或一個幾本書
我以爲我會添加這個筆記:我正在學習這個爲了爲我的業務構建一些應用程序,我是一個在wpf和xaml中的初學者,所以我改變了這可能是件愚蠢的事情。我真的很想找到一個關於datacontexts的好教程,因爲我發現十幾個不同的「How To's」完全不同。我知道在我的知識中有一些大的漏洞,因爲當我嘗試在代碼隱藏文件Window1.xaml和app.xaml文件中創建對我的datacontext的引用時,我總是會收到viewmodel類的多個實例。
我不得不做出一些改變,但這使我的解決方案。我不知道我是如何錯過了RelativeSource類的,但知道我知道。對於這個問題的其他人,爲了做到這一點,我必須將Window1.xaml.cs中的StatusBoardViewModel提升爲屬性,並將DataTrigger中的綁定更改爲Binding =「{Binding Path = StatusBoardViewModel.ShowDetailListItems,RelativeSource = {RelativeSource FindAncestor, AncestorType = {x:Type Window}}}「 – 2010-01-14 22:45:06