1、当IsMouseOver触发器不起作用时首先看是不是父级控件Stackpanel没有背景色,因为没有背景色时悬浮到下层控件,所以需要再外层加上Background="Transparent" 就可以了
<DataTemplate DataType="{x:Type xxx}">
<StackPanel Orientation="Horizontal" Height="30"
Background="Transparent">
<Border CornerRadius="4" Width="150">
<StackPanel Orientation="Horizontal">
<Button x:Name="Delete" DataContext="{Binding }" Visibility="Hidden"
HorizontalAlignment="Left" Margin="10,0,0,0"
Style="{StaticResource btnStyleImageBgTransparent}"
Background="{DynamicResource tree_delete}" Tag="{DynamicResource tree_delete_hover}"
Width="24" Height="24"/>
</StackPanel>
</Border>
</StackPanel>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true"> Property="Visibility" Value="Visible"/>
<Setter TargetName="Delete" Property="Visibility" Value="Visible"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
2、wpf textbox右键设置不可以剪切
<TextBox Name="textBox1" Height="23" Width="120">
<TextBox.ContextMenu>
<ContextMenu Visibility="Collapsed" />
</TextBox.ContextMenu>
</TextBox>
3、如果你的数组或list<double>又需要连续,则设置值为double.NaN即可
4、 static CustomButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
}
在CustomButton的静态函数中重载DefaultStyleKeyProperty内部Metadata的含义是告诉WPF系统,查找CustomButton的ThemeStyle使用的键值从{x:Type CustomButton}被改成了{x:Type CustomButton}
5、WPF 移植界面框架但是效果没有显示 ([assembly:ThemeInfo(ResourceDictionaryLocation.None,ResourceDictionaryLocation.)
我是移植的别人的操作界面,但是界面风格没有显示出来,最后一点一点对和移植界面的区别
如果需要用到Themes/Generic.xaml作为默认风格资源文件,不要忘了该项目的AssemblyInfo.cs中必须要有以下这段:
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
否则,程序将自动取当前操作系统风格。这段没有的原因很可能是WPF项目是从别处拷来而不是直接在vs里创建的。
6、图片在项目中作为资源,打包时会自动打包到dll库中
cs后台获取xaml中staticresource资源通过Application.Current.MainWindow.FindResource(key);
7 .NET Framework迁移到.NET Core方案
方案一、新建项目拷贝。
方案二、编辑.csproj文件,下面演示方案二编辑csproj文件迁移方式
1.右键指定项目“卸载项目”
2.右键编辑项目.csproj文件,删除文件全部内容
类库项目粘贴以下配置:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
</Project>
WinForm应用程序粘贴以下配置:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>
</Project>
3.删除项目中的"Properties"文件夹和文件
4.重新加载项目
5.生成项目根据错误信息重新引用依赖项
8.获取子控件方法
public List GetChildObjects(DependencyObject obj, Type typename) where T : FrameworkElement
{
DependencyObject child = null;
List childList = new List();
for (int i = 0; i {
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).GetType() == typename))
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects(child,typename));
}
return childList;
}
ListlistButtons = GetChildObjects(parentPanel, typeof(Button));
public List GetChildObjects(DependencyObject obj, string name) where T : FrameworkElement
{
DependencyObject child = null;
List childList = new List();
for (int i = 0; i {
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Name == name |string.IsNullOrEmpty(name)))
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects(child,name));
}
return childList;
}
ListlistButtons = GetChildObjects(parentPanel, "button1");
9、禁止拖拽Datagrid表头宽度设置
CanUserResizeColumns="False"
10、列标题过长 datagrid 没有数据时如何添加滚动
<ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<DataGrid x:Name="testDateGrid">
</DataGrid>
</ScrollViewer>