[英]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属性( MinWidth
, MaxWidth
, Width
)并将其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.