WPF 实战问题解决

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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个月太忙没时间看C++

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值