WPF usercontrol - 将DependencyProperty值写回XAML

时间:2013-10-17 08:03:52

标签: c# wpf xaml user-controls dependency-properties

我有一个包含标签和文本框的usercontrol。标签和文本框并排放置。我有一个名为LabelWidth的DependencyProperty,它绑定到标签宽度以调整标签宽度。

现在,当我使用VisualStudio Designer调整控件大小时,我希望控件在按下Crtl时按比例缩放。一些代码:

void Control_SizeChanged(object sender, SizeChangedEventArgs e)
{
    bool labelResizeMode = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
    if (labelResizeMode)
    {
        if (e.PreviousSize.Width > 0 && e.PreviousSize.Height > 0)
        {
            double diff = e.NewSize.Width - e.PreviousSize.Width;
            this.LabelWidth += diff;
        }
    }
}

public double LabelWidth
{
    get { return (double)GetValue(LabelWidthProperty); }
    set { SetValue(LabelWidthProperty, value); }
}

public static readonly DependencyProperty LabelWidthProperty =
        DependencyProperty.Register("LabelWidth", typeof(double), typeof(MyUserControl), new PropertyMetadata(100.0));

问题是,在调整控件大小时,LabelWidth的值不会写回xaml代码。我怎样才能做到这一点?

0 个答案:

没有答案