问题:
最近我在做一个WPF的小软件,软件有一个主界面和几个子界面。主界面是window,子界面是page。page通过Frame镶嵌到主界面window上。
<!--工作窗口-->
<TabControl Grid.Row="1">
<TabItem Header="生产">
<Frame Content="{Binding ProductPage}"/>
</TabItem>
<TabItem Header="操作">
<Frame Content="{Binding MonitorPage}"/>
</TabItem>
<TabItem Header="参数">
<Frame Content="{Binding ParamsPage}"/>
</TabItem>
<TabItem Header="日志">
<Frame Content="{Binding LogPage}"/>
</TabItem>
</TabControl>
Frame的Content都是绑定的MainViewModel中的属性,到这里都没有问题。问题出在其中子界面里。在子界面ParamsPage中有一个DataGrid用来呈现参数,ParamsPage的VM和设置方法如下:
public partial class ParamersPage : Page
{
public ParamersPage(IViewModel paramViewModel)
{
InitializeComponent();
this.DataContext = paramViewModel;
}
}
STR就是一个包含字串的字典,想用它来显示自定义文字。
public ParamViewModel(ParamManager param,StringMapping stringMapping)
{
paramManager = param;
m_Str = stringMapping;
CurrentParams = paramManager.PrTable.PrList;
}
public List<Pr> CurrentParams
{
get
{
return currentParams;
}
set
{
currentParams = value;
OnPropertyChanged("CurrentParams");
}
}
public StringMapping STR
{
get
{
return m_Str;
}
}
xaml中对应的表格如下
<DataGrid x:Name="Grid_Param" ItemsSource="{Binding CurrentParams}" Style="{StaticResource ResourceKey=ParamDataGrid}" CellStyle="{StaticResource ParamDataGridCell}" VirtualizingPanel.VirtualizationMode="Standard" RowHeaderWidth ="0" Grid.Column="1" FontSize="14" BorderThickness="1" AutoGenerateColumns="False" SelectionMode="Single"
AlternationCount="2" Margin="5,5,5,5"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" >
<DataGrid.Columns>
<DataGridTemplateColumn Width="50" IsReadOnly="True">
<DataGridTemplateColumn.Header>
<TextBlock Text ="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page},Path=DataContext.STR[ParamersPage:No]}" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}},Converter={StaticResource MyIndexConverter}}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=ID}" Width="70" IsReadOnly="True" >
<DataGridTextColumn.Header>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page},Path=DataContext.STR[ParamersPage:ID]}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Description}" Width="15*" IsReadOnly="True">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page},Path=DataContext.STR[ParamersPage:Description]}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Value, Mode=TwoWay}" Width="70" >
<DataGridTextColumn.Header>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page},Path=DataContext.STR[ParamersPage:Value]}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
表格里各列显示参数的序号,id,描述,数值都是正常的,唯独Column的Header无法正常显示。查看vs提示是
“错误 1 null DataContext.STR[ParamersPage:ID] TextBlock.Text String 找不到源: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Page', AncestorLevel='1'。”
TextBlock的绑定向上查找时找不到page。
问题点可能在于这个子界面所在的Tabitem不是软件打开时载入的初始界面?我尝试把这个界面移动到第一个tabitem让它在软件打开时就能显示出来,结果发现果然就正常显示。
尝试了很多不同的绑定方法,在page没有第一时间显示出来的情况下都无法正常显示。感觉是视图和绑定源初始化的时间有关系,对这方面不是很理解,希望有类似经验的朋友帮忙解解惑!