1. 对于ListView、ListBox等这些派生自ItemsControl的类,它们的子item的button之类的控件如何绑定到父page或者父window的ViewModel上?
Command="{Binding DataContext.BackupCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Page}}}"
如果父布局是window,则将x:Type Page
改为x:Type Windows
.
2. ListBox中的Item当被遮挡了一部分时,点击此item后会先把位置往上移,再次点击才执行相应操作,会让用户误以为一次点击没有效果,需要点击两次。
解决方法是:将ListBox的style中的ScrollView的CanContentScroll
属性的值设置为false。
3. 带Hint的TextBox控件
设置TextBox控件的Template属性为:
<ControlTemplate x:Key="HintTextBoxControlTemplate" TargetType="{x:Type TextBoxBase}">
<Border CornerRadius="10" x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<Grid>
<TextBlock x:Name="hint" Foreground="LightGray" Margin="10" Text="{TemplateBinding Tag}"></TextBlock>
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Opacity" TargetName="hint" Value="0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
把Hint的文字,写在对应的TextBox的Tag
标签中。
4.控件选中时设置以控件中心为旋转点
<Image RenderTransformOrigin="0.5,0.5" />
5. 使用Clip对非Border控件设置圆角
样式:
<StackPanel Background="#3b466c" Height="230" Width="320" >
<StackPanel.Clip>
<RectangleGeometry RadiusX="8" RadiusY="8">
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource GetRectCvt}" >
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=StackPanel}"/>
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=StackPanel}"/>
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
</StackPanel.Clip>
</StackPanel>
转化器:
public class GetRectConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values?.Length == 2)
{
return new Rect(0, 0, (double)values[0], (double)values[1]);
}
return new Rect(0, 0, 100, 100);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return null;
}
}
6. 手动设置WindowState之后,窗口的Left和Top位置不对?
手动获取_actualLeft
和_actualTop
即可:
public static double GetActualLeft(this Window window)
{
var f = typeof(Window).GetField("_actualLeft", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
return (double)f.GetValue(window);
}
public static double GetActualTop(this Window window)
{
var f = typeof(Window).GetField("_actualTop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
return (double)f.GetValue(window);
}
7. Template、ItemTemplate、ItemContainerStyle的区别
8. DragDrop拖拽相关的几个事件
源控件上需要设置:
-
MouseMove
:调用DragDrop.DoDragDrop(obj,obj,obj)
设置要传送的数据。 -
GiveFeedback
:可更改鼠标指针外观。持续触发。需要标记为已处理。 -
QueryContinueDrag
:判断用户是否已取消。持续触发。
目标控件上需要设置:
-
DragEnter
:可设置Effects
为none
,以拒绝某种类型。
private void lblTarget_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Text))
e.Effects = DragDropEffects.Copy;
else
e.Effects = DragDropEffects.None;
}
-
DragLeave
: -
DragOver
:类似于MouseMove
,在目标控件上移动时持续触发。也可设置Effects
为none
,以拒绝某种类型。 -
AllowDrop
:应设置为true
。 -
Drop
:处理拖拽。 如下:
private void gridFiles_Drop(object sender, DragEventArgs e)
{
txtTotalCount.Text = "";
var formats = e.Data.GetFormats();
if (e.Data.GetDataPresent("FileDrop"))
{
_filePaths = (string[])e.Data.GetData("FileDrop");
txtTotalCount.Text = $"total:{_filePaths?.Count()}";
}
}
9. DockerManager结构相关
10. WPF中的空域问题处理
参考:https://github.com/MahApps/MahApps.Metro/wiki/Why-is-WindowsFormsHost-or-WebBrowser-content-annoying-me%3F文章来源:https://www.toymoban.com/news/detail-513833.html
11. WPF中如何让WindowsFormsHost透明?
https://blog.csdn.net/x740073529/article/details/124315348文章来源地址https://www.toymoban.com/news/detail-513833.html
到了这里,关于【WPF】wpf笔记本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!