WPF WindowsFormsHost 控件不工作显示错误
WPF WindowsFormsHost control is not woking showing error
我正在使用 WindowsFormsHost
在我的 WPF 应用程序中显示 DataGridView
。
<wfi:WindowsFormsHost Name="winhost" Grid.Row="0" Width="930" Height="200" Margin="5,5,5,0" Visibility="Hidden">
<wf:DataGridView x:Name="dataGridViewOutlookMailList" ScrollBars="Both" AllowUserToAddRows="False" RowHeadersVisible="False" CellContentClick="dataGridViewOutlookMailList_CellContentClick">
<wf:DataGridView.Columns>
<wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn3" HeaderText="" Width="25" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn13" HeaderText="Outlook Entry ID" Width="100" Visible="false" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn14" HeaderText="FullPath With FileName" Width="100" Visible="False" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn15" HeaderText="Resume Name" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn18" HeaderText="Sender Name" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn17" HeaderText="Received By Name" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn16" HeaderText="Received" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn19" HeaderText="Sender Address" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn20" HeaderText="To" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn21" HeaderText="Bcc" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn22" HeaderText="Cc" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn23" HeaderText="Subject" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn24" HeaderText="Body" Width="100" Visible="True" />
<wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn4" HeaderText="NotesActivity" Width="25" Visible="False" />
<wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn5" HeaderText="Body As Attachment" Width="100" Visible="False" />
</wf:DataGridView.Columns>
</wf:DataGridView>
当鼠标悬停在 datagridview 上时出现以下错误,而且 UI 未正确加载。有关详细信息,请参阅屏幕截图。
我也在使用下面的代码
WinForms.DataGridViewCellStyle ColStyle1 = new WinForms.DataGridViewCellStyle();
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
pfc.AddFontFile(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\NewZrr\OpenSans-Regular.ttf");
foreach (WinForms.DataGridViewColumn c in dataGridViewOutlookMailList.Columns)
{
c.DefaultCellStyle.Font = new System.Drawing.Font(pfc.Families[0], 8, System.Drawing.FontStyle.Regular);
}
pfc = null;
在我的 Window_Loaded 方法中更改 datagridview 的字体。请指导我解决问题。
问题是 pfc
变量中的 PrivateFontCollection
实例超出了范围,所以当 DataGridViewCell
第一次绘制时,无法正确加载指定的字体.
将pfc
实例移到Window_Loaded
方法之外,防止GC回收,并且不设置为null,使其成为长寿命对象。
参考:Parameter is not valid when draw text in label with custom font
我正在使用 WindowsFormsHost
在我的 WPF 应用程序中显示 DataGridView
。
<wfi:WindowsFormsHost Name="winhost" Grid.Row="0" Width="930" Height="200" Margin="5,5,5,0" Visibility="Hidden">
<wf:DataGridView x:Name="dataGridViewOutlookMailList" ScrollBars="Both" AllowUserToAddRows="False" RowHeadersVisible="False" CellContentClick="dataGridViewOutlookMailList_CellContentClick">
<wf:DataGridView.Columns>
<wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn3" HeaderText="" Width="25" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn13" HeaderText="Outlook Entry ID" Width="100" Visible="false" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn14" HeaderText="FullPath With FileName" Width="100" Visible="False" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn15" HeaderText="Resume Name" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn18" HeaderText="Sender Name" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn17" HeaderText="Received By Name" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn16" HeaderText="Received" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn19" HeaderText="Sender Address" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn20" HeaderText="To" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn21" HeaderText="Bcc" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn22" HeaderText="Cc" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn23" HeaderText="Subject" Width="100" Visible="True" />
<wf:DataGridViewTextBoxColumn Name="dataGridViewTextBoxColumn24" HeaderText="Body" Width="100" Visible="True" />
<wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn4" HeaderText="NotesActivity" Width="25" Visible="False" />
<wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn5" HeaderText="Body As Attachment" Width="100" Visible="False" />
</wf:DataGridView.Columns>
</wf:DataGridView>
当鼠标悬停在 datagridview 上时出现以下错误,而且 UI 未正确加载。有关详细信息,请参阅屏幕截图。
我也在使用下面的代码
WinForms.DataGridViewCellStyle ColStyle1 = new WinForms.DataGridViewCellStyle();
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
pfc.AddFontFile(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\NewZrr\OpenSans-Regular.ttf");
foreach (WinForms.DataGridViewColumn c in dataGridViewOutlookMailList.Columns)
{
c.DefaultCellStyle.Font = new System.Drawing.Font(pfc.Families[0], 8, System.Drawing.FontStyle.Regular);
}
pfc = null;
在我的 Window_Loaded 方法中更改 datagridview 的字体。请指导我解决问题。
问题是 pfc
变量中的 PrivateFontCollection
实例超出了范围,所以当 DataGridViewCell
第一次绘制时,无法正确加载指定的字体.
将pfc
实例移到Window_Loaded
方法之外,防止GC回收,并且不设置为null,使其成为长寿命对象。
参考:Parameter is not valid when draw text in label with custom font