2
如下圖所示,我有一個彈出窗口,我想用右下角的拇指調整窗口大小。拇指有一個附加的行爲,我想要調整大小。通過附加行爲的Thumb調整彈出窗口大小
<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); }
}
}
它目前沒有工作,但應該給我一個我想要的東西的好主意。
如何讓這種行爲起作用?
如果'PopupObject'是一個附加屬性,那麼它不需要定義(在行爲類中)GetPopupObject和SetPopupObject靜態方法嗎? – 2015-01-21 11:25:23
不,它工作正常。這是問題的實際拖拽和調整大小。 – Hank 2015-01-21 12:09:34