C# 通过带有附加行为的Thumb调整弹出窗口的大小
如下图所示,我有一个弹出窗口,我想在右下角用拇指调整大小。拇指有一个附加的行为,我想做的大小调整C# 通过带有附加行为的Thumb调整弹出窗口的大小,c#,wpf,attachedbehaviors,C#,Wpf,Attachedbehaviors,如下图所示,我有一个弹出窗口,我想在右下角用拇指调整大小。拇指有一个附加的行为,我想做的大小调整 <Popup x:Name="Popbox" Placement="Mouse" StaysOpen="False" Width="50" Height="50" > <Grid> <Border Background="AliceBlue"/> <Thumb HorizontalAlignment="Right"
<Popup x:Name="Popbox" Placement="Mouse" StaysOpen="False" Width="50" Height="50" >
<Grid>
<Border Background="AliceBlue"/>
<Thumb HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Width="16" Height="16" >
<i:Interaction.Behaviors>
<helpers:PopupResizeBehaviors PopupObject="{Binding ElementName=Popbox}"/>
</i:Interaction.Behaviors>
</Thumb>
</Grid>
</Popup>
class PopupResizeBehaviors : Behavior<Thumb>
{
private bool mouseDown;
private Point oldMousePosition;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewMouseLeftButtonDown += (s, e) =>
{
mouseDown = true;
};
AssociatedObject.DragDelta += (s, e) =>
{
if (!mouseDown) return;
double tempWidth = 0;
double tempHeight = 0;
PopupObject.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
tempWidth = PopupObject.DesiredSize.Width;
tempHeight = PopupObject.DesiredSize.Height;
double yadjust = tempHeight + e.VerticalChange;
double xadjust = tempWidth + e.HorizontalChange;
PopupObject.Width = xadjust;
PopupObject.Height = yadjust;
};
AssociatedObject.PreviewMouseLeftButtonUp += (s, e) =>
{
mouseDown = false;
};
}
protected override void OnDetaching()
{
base.OnDetaching();
}
public static readonly DependencyProperty PopupObjectProperty =
DependencyProperty.RegisterAttached("PopupObject", typeof(Popup), typeof(PopupResizeBehaviors), new UIPropertyMetadata(null));
public Popup PopupObject
{
get { return (Popup)GetValue(PopupObjectProperty); }
set { SetValue(PopupObjectProperty, value); }
}
}
类PopupResizeBehaviors:行为
{
私人布尔·穆斯敦;
私密点鼠标定位;
受保护的覆盖无效附加()
{
base.onatached();
AssociatedObject.PreviewMouseLeftButtonDown+=(s,e)=>
{
mouseDown=true;
};
AssociatedObject.DragDelta+=(s,e)=>
{
如果(!mouseDown)返回;
双tempWidth=0;
双温度高度=0;
度量(新大小(Double.PositiveInfinity,Double.PositiveInfinity));
tempWidth=PopupObject.DesiredSize.Width;
tempHeight=PopupObject.DesiredSize.Height;
双yadjust=温度高度+e.垂直变化;
双xadjust=tempWidth+e.水平变化;
PopupObject.Width=xadjust;
PopupObject.Height=yadjust;
};
AssociatedObject.PreviewMouseLeftButtonUp+=(s,e)=>
{
mouseDown=false;
};
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
}
公共静态只读DependencyProperty PopupObjectProperty=
DependencyProperty.RegisterAttached(“PopupObject”、typeof(弹出)、typeof(PopupResizeBehaviors)、新UIPropertyMetadata(null));
公共弹出式弹出对象
{
get{return(Popup)GetValue(PopupObjectProperty);}
set{SetValue(PopupObjectProperty,value);}
}
}
它目前不工作,但应该给我的目标一个很好的想法
如何让这种行为起作用?
这就是我最后使用的
class PopupResizeBehaviors : Behavior<Thumb>
{
private const int MaxSize = 500;
private const int MinSize = 50;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.DragDelta += (s, e) =>
{
Thumb t = s as Thumb;
if (t.Cursor == Cursors.SizeWE || t.Cursor == Cursors.SizeNWSE)
{
PopupObject.Width = Math.Min(MaxSize,
Math.Max(PopupObject.Width + e.HorizontalChange,
MinSize));
}
if (t.Cursor == Cursors.SizeNS || t.Cursor == Cursors.SizeNWSE)
{
PopupObject.Height = Math.Min(MaxSize,
Math.Max(PopupObject.Height + e.VerticalChange,
MinSize));
}
};
}
protected override void OnDetaching()
{
base.OnDetaching();
}
public static readonly DependencyProperty PopupObjectProperty =
DependencyProperty.RegisterAttached("PopupObject", typeof(Popup), typeof(PopupResizeBehaviors), new UIPropertyMetadata(null));
public Popup PopupObject
{
get { return (Popup)GetValue(PopupObjectProperty); }
set { SetValue(PopupObjectProperty, value); }
}
}
类PopuPresizeBehavior:行为
{
私有常量int MaxSize=500;
私有const int MinSize=50;
受保护的覆盖无效附加()
{
base.onatached();
AssociatedObject.DragDelta+=(s,e)=>
{
拇指t=s为拇指;
if(t.Cursor==Cursors.SizeWE | | t.Cursor==Cursors.SizeNWSE)
{
PopupObject.Width=Math.Min(MaxSize,
Math.Max(PopupObject.Width+e.HorizontalChange,
MinSize);
}
if(t.Cursor==Cursors.SizeNS | | t.Cursor==Cursors.SizeNWSE)
{
PopupObject.Height=Math.Min(MaxSize,
数学最大值(PopupObject.Height+e.VerticalChange,
MinSize);
}
};
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
}
公共静态只读DependencyProperty PopupObjectProperty=
DependencyProperty.RegisterAttached(“PopupObject”、typeof(弹出)、typeof(PopupResizeBehaviors)、新UIPropertyMetadata(null));
公共弹出式弹出对象
{
get{return(Popup)GetValue(PopupObjectProperty);}
set{SetValue(PopupObjectProperty,value);}
}
}
如果PopupObject
是一个附加属性,那么它不需要GetPopupObject
和SetPopupObject
定义的静态方法(在您的行为类中)?不,它工作正常。问题在于实际的拖动和大小调整。