wpf的一些总结

wpf技巧

隐藏控件不占空间,设置visibility为:Collapsed

tabcontrol的高度宽度跟随界面的大小变化:属性heightwidth绑定grid的actualheightactualwidth,xaml代码Width="{Binding ActualWidth, ElementName=grid}" Height="{Binding ActualHeight, ElementName=grid}"

控制控件所在的StackPanel,就能控制多个控件的属性。如设置StackPanel的isEnable为false,那么此StackPanel下的控件的isEnable都未false

xaml和后台映入命名空间的方式不同,比如引入拥有 Triggers 和 Behaviors 附加属性的静态类——Interaction类 。 后台命名空间引入:using System.Windows.Interactivity

xaml引入:xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity

datagrid列重复,将AutoGenerateColumns设为false即可

操作excel。添加office、excel dll,项目右键引用、com里搜索,读取导出excel所需命名空间:引用com using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

读取dataset DataSet.Tables[0].Rows[0]["username"].ToString()

Popup显示位置的设置 IsOpen StaysOPen Placement="Mouse"(在鼠标点击处打开)

获取datagrid所在行id:DataRowView ds = myDataGrid.SelectedItem as DataRowView;
MessageBox.Show(ds.Row[0].ToString());
修改datagrid一行即引发的事件为RowEditEnding

转义字符串,加个@号

string str=@"df
sadf
sdf";

设置全局样式:

在app.xaml的resourcedictionary节点里加上
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDic/MainResourceDictionary.xaml"/>
<ResourceDictionary Source="Themes/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
在style.xaml里定义样式,比如<Style x:Key="s1" TargetType="TextBox">
<Setter Property="Background" Value="#FFEAEAEA"></Setter>
</Style>

TextBox引用Style="{StaticResource s1}"即可


将button抽出来放在单独的文件,可以在blend里选中button,右键剪辑样式-编辑副本

WCF

默认情况下,BasicHttpBinding发送的是明文数据,而WsHttpBinding发送的是加密和更加安全的数据。
契约相当于公司与客户之间签订的合同
DataContract需要引用命名空间system.Runtime.Serialization并using到cs文件中
Reference.cs出错,很多处错误,发现是引用没更新的原因
服务端定义客户端的各种东西,比如绑定、终结点、服务、行为等
有趣的单向双向通讯,在operationcontract操作契约中的isOneWay是true或否中决定

WCF服务,先是定义服务协定,而服务协定中会有若干个服务操作协定(OperationContract),是这样吧?而所谓的操作协定,就是一个方法。

协定有数据协定DataContract,服务协定ServiceContract,信息协定MessageContract

静态对象会导致new窗体时发生xamlparseexception错误

调试器无法继续运行该进程。无法启动调试 卸载部署项目并重新加载项目即可

打包文件会出现的错误

右击setup项目,点击install出现错误:
错误-1014: Cannot rename directory E:开发项目MyWPFProjectSetup1Setup1ExpressSingleImage to E:开发项目MyWPFProjectSetup1Setup1ExpressSingleImage.Bak.
Windows Explorer or a DOS prompt may be pointing to a subfolder of the release output folder (Disk1) or to the Interm folder, locking it. Change the current directory. Close any open files in the Disk1 folder. Close Msidb.exe if it is open.
删除SingleImage文件夹即可

调试setup打包项目,出现“调试器无法继续运行该进程。无法启动调试“错误,右键卸载打包项目再重新加载即可

样式


button圆角:<Button x:Name="queryTB" Margin="0,0,6,0" Content="{DynamicResource query}" BorderBrush="#FFB0BED2" BorderThickness="0.5,1,0.5,0.3" Foreground="#FF294364" >
<Button.Template >
<ControlTemplate TargetType="{x:Type Button}" >
<Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="1" CornerRadius="3,3,3,3">
<Border.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFDFEBF9" Offset="0"/>
<GradientStop Color="#FFD2E0EF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" ></ContentPresenter>
</Border>
</ControlTemplate>
</Button.Template>

command传递控件本身

<TextBox x:Name="usernameTB" Grid.Row="0" Grid.Column="1" MinWidth="133" MaxWidth="228" BorderBrush="#FF8BA0BC" LostFocus="usernameTB_LostFocus" TextChanged="usernameTB_TextChanged" Style="{DynamicResource TextBoxStyleOne}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<i:InvokeCommandAction Command="{Binding GotfocusCommand}" CommandParameter="{Binding ElementName=usernameTB}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>

ViewModel
if (textbox.GetType().ToString() == "System.Windows.Controls.TextBox")
{
TextBox tb = textbox as TextBox;
if (tb != null)
{
// System.Windows.MessageBox.Show("111"+tb.Name);
tb.Style = tb.TryFindResource("s1") as Style;
}
}

原文地址:https://www.cnblogs.com/ssvip/p/7275778.html