我这里有一点问题。我将我的datatemplate声明在xaml的顶部并在listbox itemtemplate中链接到它。 XAML
<DataTemplate x:Key="listTemplate">
<!--In case we need a click add button -->
<DockPanel HorizontalAlignment="Left" Height="92" LastChildFill="False" Margin="17,10,0,0" VerticalAlignment="Top" Width="615" Background="Green" >
<Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8" Margin="10,0,614,475"/>
<TextBlock Margin="10,42,10,29" Width="70"> Приміщення</TextBlock>
<TextBlock Margin="10,42,10,29" Width="75"> №</TextBlock>
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
<TextBlock Margin="10,42,10,29" Width="91"> Гамма</TextBlock>
<TextBlock Margin="10,42,10,29" Width="91" Text="{Binding MonitorGammaCh}"></TextBlock>
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
<TextBlock Margin="10,42,10,29" Width="91"> Нейтрон</TextBlock>
<TextBlock Margin="10,42,10,29" Width="72" Text="{Binding MonitorNeutronCh}"></TextBlock>
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
</DockPanel>
<!--</StackPanel>-->
</DataTemplate>
<TabItem x:Name="MonitorsTab" Background="#617891" >
<TabItem.Header>
<Image Width="100" Height="100" Source="../Resources/radioactive 256X256.png" />
</TabItem.Header>
<ListBox x:Name="MonitorsList" ItemTemplate="{StaticResource listTemplate}">
</ListBox>
</TabItem>
在我的代码中我有
MonitorsTab.DataContext = mainWindowViewModel.MonitorsTabViewModel;
MonitorsViewModels = new ObservableCollection<MonitorViewModel>
{
new MonitorViewModel { MonitorGammaCh = "gamma1", MonitorNeutronCh = "neutron2" } ,
new MonitorViewModel { MonitorGammaCh = "gamma1", MonitorNeutronCh = "neutron2" },
//new MonitorViewModel { MonitorGammaCh = "gamma1", MonitorNeutronCh = "neutron2"}
};
MonitorsList.ItemsSource = MonitorsViewModels;
我得到的结果就是这个。没有可见的gamma1和neutron2文本
我想知道为什么Resharper找不到上下文。
答案 0 :(得分:1)
ReSharper不会从代码隐藏中推断出DataContext。您需要在XAML中使用d:DataContext
属性显式设置设计时DataContext,如下所示:
<Window x:Class="WpfApplication1.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance wpfApplication1:MainWindowViewModel}">
</Window>
设置后,ReSharper将解析所有与DataContext相关的符号,并为您提供智能感知,导航和其他功能。