WPF 开发技巧
自定义高性能无边框窗体
引入Microsoft.Windows.Shell。可参考 WPF Custom Chrome Library 和 MSDN WindowChrome Class 有相关自定义窗口实现。
参考:https://www.cnblogs.com/fishpond816/p/14299208.html
MVVM模式
参考:https://www.cnblogs.com/fishpond816/p/13467435.html
WPF 进程间传递参数
/***************************
* 启动附程序时传递参数
* Process.Start(exePath,"测试字符串");
*
* 附程序使用 Application_Startup 接收参数
*/
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Length == 1)
{
this.Properties["Test"] = e.Args[0];
}
}
WPF CommandParameter 传参
使用控件本身作为参数
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
使用当前窗口作为参数
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
当前DataContext对象
CommandParameter="{Binding UserName}"
Binding
Text="{Binding UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
空白汉字占位符,  表示一个汉字占位符
<TextBlock xml:space="preserve" Text="备  注" />
绑定元素
<ProgressBar x:Name="progressBar"/>
<Label Content="{Binding ElementName=progressBar,Path=Value}" ContentStringFormat="{}{0:P2}"/>
绑定属性
<Label Content="{Binding Path=Result,Mode=TwoWay}"/>
Binding后台实现
Binding binding = new Binding();
binding.Source = System.Diagnostics.Process.GetCurrentProcess();
binding.Path = new PropertyPath("ProcessName");
binding.Mode = BindingMode.OneWay;
txtOne.SetBinding(TextBlock.TextProperty,binding);
绑定列表
ObservableCollection<Object> infos = new ObservableCollection<Object>();
如果想对于对象属性修改也通知到UI,需要类实现INotifyPropertyChanged接口
异步更改UI
Action action = () => { };
Application.Current.Dispatcher.Invoke(action);
Application.Current.Dispatcher.BeginInvoke(action);
绑定注意细节
数据源初始化不要为null
数据源实例化之后不要再次实例化

浙公网安备 33010602011771号