wpf上传控件
❶ 如何在WPF中嵌入Winform控件
1. 新建一个基于.Net Framework 3.5 的WPF 应用程序项目:WPFWMP。
2. 在工程中新建Windows Forms Control Library 项目:WMPControlLibrary。
创建WMP 控件
下面要在WMPControlLibrary 中创建Windows Media Player 控件,在项目中加入Windows Media Player COM。
在左侧工具栏中若没有Windows Media Player 控件的话,可以右键General 选择Choose Items,在COM 组件列表中勾选Windows Media Player 选项。
将Windows Media Player 控件拖入设计窗口,并将Dock 设置为Fill 填充控件。
F6 编译项目后会生成以下三个DLL 文件,这就是我们稍后将要在WPF 中用到的WMP 控件库。
嵌入WMP 控件
回到WPF 项目在前篇文章的基础上,保留其中“Open File” 按键和Button 样式。将上面三个DLL 文件及System.Windows.Forms、WindowsFormsIntegration 加入项目。
在XAML 中加入AxWMPLib 命名空间,并将上篇MediaElement 替换为AxWindowsMediaPlayer 控件,注意此处是将WinForm 控件嵌入WPF 程序,所以要将AxWindowsMediaPlayer 控件放到<WindowsFormsHost>标签中。
<Window x:Class="WPFWMP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mediaControl="clr-namespace:AxWMPLib;assembly=AxInterop.WMPLib"
Title="WPF Media Player" Height="450" Width="520" Background="#FF554D4D">
<Window.Resources>
<Style x:Key="btnStyle" TargetType="Button">
… …
</Style>
</Window.Resources>
<StackPanel HorizontalAlignment="Center" Margin="10">
<Border BorderThickness="3" Background="Black">
… …
<WindowsFormsHost Height="340" Width="450">
<mediaControl:AxWindowsMediaPlayer x:Name="wpfMediaPlayer"/>
</WindowsFormsHost>
</Border>
<Button Content="Open File" Click="openFile_Click" Margin="10"
Width="80" Style="{StaticResource btnStyle}"/>
</StackPanel>
</Window>
通过Windows API Code Pack 为“Open File” 按键添加点击事件,默认打开Sample Video 文件夹,选择视频文件后自动播放。
private void openFile_Click(object sender, RoutedEventArgs e)
{
ShellContainer selectedFolder = null;
selectedFolder = KnownFolders.SampleVideos as ShellContainer;
CommonOpenFileDialog cfd = new CommonOpenFileDialog();
cfd. = selectedFolder;
cfd.EnsureReadOnly = true;
cfd.Filters.Add(new CommonFileDialogFilter("WMV Files", "*.wmv"));
cfd.Filters.Add(new CommonFileDialogFilter("AVI Files", "*.avi"));
cfd.Filters.Add(new CommonFileDialogFilter("MP3 Files", "*.mp3"));
if (cfd.ShowDialog() == CommonFileDialogResult.OK)
{
wpfMediaPlayer.URL = cfd.FileName;
}
}
❷ 如何在WPF中调用Winform控件
功能实现主要分三步:
1、添加两个引用:WindowsFormsIntegration.dll(负责整合WPF和Windows)、System.Windows.Forms.
2、在 XAML文件中添加两个引用(粗体部分):
<Window x:Class="CrossBowDemo.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hosting Windows Forms Control In WPF"
Height="300"
Width="650"
ResizeMode="NoResize"
Loaded="WindowLoadedHandler"
>
< /Window>
3、在XAML编码区实现你想添加的控件:
原文添加的是DataGridView控<wfi:WindowsFormsHost>
<wf:DataGridView x:Name="Dg" Dock="Fill" SelectionMode="FullRowSelect">
</wf:DataGridView>
</wfi:WindowsFormsHost>件:
❸ wpf添加新控件原本的控件消失
动态添加控件——在后台实例化需要添加的控件,然后将这个控件放进容器里
例如Grid.children.add(控件)
原本的控件消失——有两种做法 1.将控件的visibility 设为collapse 2.将容器里这个控件移除
例如Grid.children.remove(控件)
以上这两个逻辑操作放在一个事件里完成就行啦。
❹ WPF 动态添加控件与设置控件模版
首先呢,DataTemplate作为Resource的一种呢,是用key来被识别的,不用设置name的。
其次我来说一下关于你两个问题的一些看法。
问题一——你这种问法相当于如何在后台对一个控件的Content添加一个控件。一般解决的方法为
后台先创建好listbox,然后赋值 expander1.Content=listbox;
问题二:如何后台指定控件模板。你后台创建一个listbox。 那就可以像一般属性一样赋给它的控件模板呀。
listbox.ItemTemplate = Resources["lbItemTemp"] as DataTemplate;