简体   繁体   English

如何使用代码隐藏更改C#中添加到画布的UserControl的宽度?

[英]How do I change the width of a UserControl that is added to a canvas in C# using code-behind?

Using Visual Studion 2015, C# Metro Style application Working only in Code Behind. 使用Visual Studion 2015,C#Metro Style应用程序仅在“隐藏代码”中工作。

Problem: I have added a userControl to a canvas. 问题:我已将userControl添加到画布。 I want to change width of the control. 我想更改控件的宽度。

The canvas provides two functions to move the UserControls: Canvas.SetLeft, Canvas.SetTop 画布提供了两种移动UserControls的功能:Canvas.SetLeft,Canvas.SetTop

These two functions work correctly, so I know its possible to access the controls properties. 这两个功能正常工作,因此我知道可以访问控件属性。 I am trying to access the width property for my UserControl (StickyNote) to size the control on the Canvas. 我正在尝试访问UserControl(StickyNote)的width属性,以在Canvas上调整控件的大小。 StickyNote is set to 200x200 size by default, when it is added to the canvas. 将StickyNote添加到画布时,默认情况下将其设置为200x200大小。

Control_ManipulationDelta is called when I move the mouse cursor over the UserControl on the canvas. 当我将鼠标光标移到画布上的UserControl上时,将调用Control_ManipulationDelta。 This code is in my Main.xaml page. 这段代码在我的Main.xaml页面中。

private void Control_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    // StickyNote is my custom class that is added to the canvas
    StickyNote myControl2 = (StickyNote)sender;

    // Calculating the new location of the mouse
    xPoint = xPoint + e.Delta.Translation.X;
    yPoint = yPoint + e.Delta.Translation.Y;

    // Setting the controls left and top based on mouse position
    Canvas.SetLeft(myControl2, Canvas.GetLeft(myControl2) + xPoint);
    Canvas.SetTop(myControl2, Canvas.GetTop(myControl2) + yPoint);

    // Resetting our points
    xPoint = 0;
    yPoint = 0;

    // Setting Width - works to send values to custom control
    // But the canvas fails to update.
    myControl2.SetValue(WidthProperty,  800); // 500 to 800 as test. 
}

The StickyNote is a UserControl that has a text field on the XAML page. StickyNote是一个UserControl,在XAML页面上具有一个文本字段。 When SetValue for WidthProperty is called it steps into the StickNote code and update the Width to 800. Even though the Width parameter is set to 800, it does not change on the canvas. 调用WidthProperty的SetValue时,它将进入StickNote代码并将Width更新为800。即使Width参数设置为800,它在画布上也不会更改。 It shows the change in the StickyNote and in ManipulationDelta. 它显示了StickyNote和ManipulationDelta中的更改。 It is a persistent change and shows up on the next call to ManipulationDelta. 这是一个持续的更改,并在下次调用ManipulationDelta时显示。 The canvas does not update the text field to the new size. 画布不会将文本字段更新为新大小。

// here is the code that I used to setup my DependencyObject    
public  partial class StickyNote : UserControl, IPlugin, INotifyPropertyChanged
{
    // Constructor
    // Functions

    // Update control property width
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new   PropertyChangedEventArgs(propertyName));
        }
    }

    private static void OnWidthPropertyChanged(DependencyObject                    
                                  DependencyObject,  
                                  DependencyPropertyChangedEventArgs e)
   {
        StickyNote myUserControl = dependencyObject as StickyNote;
        myUserControl.OnPropertyChanged("Width");
        myUserControl.OnWidthPropertyChanged(e);
   }

   private void OnWidthPropertyChanged(DependencyPropertyChangedEventArgs e)
   { 
       Width = (double)e.NewValue;           
   }
}

Hoping someone can either show me a code sample that changes the width of custom controls that is added to a canvas, or point me in the right direction. 希望有人可以向我展示一个代码示例,该示例可以更改添加到画布上的自定义控件的宽度,也可以为我指明正确的方向。 Thank you, Ray 谢谢雷

Your text field may have absolute Width of 300 , for example. 例如,您的文本字段的绝对Width可能为300 If so, it does not matter whether you resize outer container or not. 如果是这样,是否调整外部容器的大小都没有关系。 It will always have its Width set to 300 . 它将始终将其Width设置为300 If you want your text field to change width when you resize UserControl containing it ( StickyNote ), do not specify any width property ( MinWidth , MaxWidth , Width ) on text field and set its HorizontalAlignment to Stretch . 如果要在调整包含它的UserControl的大小时( StickyNote )更改文本字段的宽度,请不要在文本字段上指定任何width属性( MinWidthMaxWidthWidth )并将其HorizontalAlignment设置为Stretch

The solution was to fix my XAML. 解决的办法是修复我的XAML。 I have hard coded sizes in the Width and Height. 我在宽度和高度上有硬编码的尺寸。 Also need to look at the Min Width and Min Height too. 还需要查看最小宽度和最小高度。 These need to be bound to the properties that I am setting using the Dependency Object. 这些需要绑定到我使用依赖对象设置的属性。 Hope this helps anyone else that might be facing similar issues. 希望这对其他可能遇到类似问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将HTML元素的值传递给C#代码隐藏方法? - How do I pass an HTML element's value to a C# code-behind method? C#如何将图像加载到代码隐藏的生成表中? - C# How Do I Load an image into a code-behind generated table? 如何从我的 aspx 页面将参数传递到 C# 代码隐藏方法? - How do I pass a parameter into a C# code-behind method from my aspx page? 如何从后面的代码中将CSS样式添加到ASP.NET UserControl? - How do I add CSS styles to an ASP.NET UserControl from the code-behind? 在设计模式下,从背后的用户控件代码中,如何获取父控件中包含的所有控件名称? - From a usercontrol code-behind, in design mode, how do I get all the controls names contained in the parent? 通过C#代码动态更改DIV位置 - Change DIV position dynamically through C# code-behind 如何更改WPF XAML以反映C#代码隐藏的变化? - How to change the WPF XAML to reflect changes in the C# code-behind? 如何在C#代码隐藏中将jQuery函数绑定到动态创建的控件 - How do you bind jQuery functions to dynamically created controls in C# code-behind 如何使用JavaScript在C#背后的代码中调用函数? - How to call function in code-behind c# with javascript? 我如何通过AngularJS视图/控制器将文件上传到C#代码背后? - How can I upload a file to C# code-behind via an AngularJS view/controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM