如何在运行时设置用户控件大小在Wpf中 [英] How Do I Set The User Control Size During Runtime In Wpf

查看:98
本文介绍了如何在运行时设置用户控件大小在Wpf中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用wpf在vb.net中开发了一个项目。在这个项目中,我只使用 USER CONTROL ,但是当我运行我的项目时,设计太过分了,即使我已经为设计高度和设计宽度分配了一个小值,显示仍然太大了。 />


有人可以帮我解决这个问题。非常感谢你们的专家!!

i这个wpf还是新手。



非常感谢你!



Xaml代码如下所示:



< usercontrol x:class =UserRoleTreeViewUCxmlns:x =#unknown >

xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x =http:// schemas。 microsoft.com/winfx/2006/xaml

xmlns:d =http://schemas.microsoft.com/expression/blend/2008

xmlns:mc =http://schemas.openxmlformats.org/markup-compatibility/2006

xmlns:telerik =http://schemas.telerik.com/2008/xaml/presentation

xmlns:userRoleManagement =clr-namespace:UserRoleManagement

d:DataContext ={d:DesignInstance Type = userRoleManagement:UserRoleTreeViewVM,

IsDesignTimeCreatable = False}

d:设计高度=259.271

d:DesignWidth =514.135

mc:Ignorable =d>

< usercontrol.resources>



< datatemplate x:key =TreeContent>

< textblock text ={Binding Title}> < br $>




< hierarchicaldatatemplate x:key =SubTree>

ItemTemplate ={StaticResource TreeContent}

ItemsSource ={Binding SubModules}>

< textblock fontweight =Mediumtext ={Binding Title}>





< Style x:Key =ExpanderStyleTargetType ={x:Type ToggleButton}>

< ; setter property =IsEnabledvalue =True>

< setter property =IsTabStopvalue =False>

< setter property = 光标值=手>

< setter property =Template>

< setter.value>

< controltemplate targettype ={x:Type ToggleButton}>

< grid>

< grid x:name =ButtonOver>

Width =16

高度=16

保证金=0,4,0,0

Horizo​​ntalAlignment =正确

VerticalAlignment =Top

Opacity =0>

< rectangle width =Auto>

Height =Auto

Horizo​​ntalAlignment =Stretch

VerticalAlignment =Stretch

RadiusX =3

RadiusY =3

Stroke =DeepPink>

< rectangle.fill>

< lineargradientbrush startpoint =0.5,0endpoint =0.5,1> < gradientstop offset =0color =#FF00BCFF>

< gradientstop offset =1color =#FF027BA6>

< gradientstop offset =0.5color =#FF00A5E0>

< gradientstop offset =0.501color =#FF005674>





< rectangle x:name =CollapsedVisualOver>

Width =2

Height =8

Margin =7,4,0,0

Horizo​​ntalAlignment =Left

VerticalAlignment =Top

Fill =#FFFFFFFF

RadiusX =0

RadiusY =0/>

< rectangle width =8 >

高度=2

保证金=4,7,0,0

Horizo​​ntalAlignment =Left

VerticalAlignment =Top

Fill =#FFFFFFFF

RadiusX =0

RadiusY =0 />



< grid x:name =Button>

Width =16

身高=16

保证金=0,4,0,0

Horizo​​ntalAlignment =正确

VerticalAlignment =Top>

< rectangle width =Auto>

Height =Auto

Horizo​​ntalAlignment =Stretch

VerticalAlignment =Stretch

Fill =#FF00A2DC

RadiusX =3

RadiusY =3

Stroke =#FF027BA6 />

< rectangle x:name =CollapsedVisual>

宽度=2

高度=8

保证金=7,4,0,0

Horizo​​ntalAlignment =左

VerticalAlignment =Top

Fill =#FFFFFFFF

RadiusX =0

RadiusY =0/>

< rectangle width =8>

高度=2

保证金=4,7,0,0

Horizo​​ntalAlignment =左

VerticalAlignment =Top

Fill =#FFFFFFFF

RadiusX =0

RadiusY =0/>







< controltemplate.triggers>

< trigger property =IsMouseOvervalue =True > < trigger.enteractions> < beginstoryboard>

< storyboard>

< doubleanimation storyboard.targetname =ButtonOver>

Storyboard.TargetProperty =( UIElement.Opacity)

To =1/>

< doubleanimation storyboard.targetname =Button>

故事板.TargetProperty =(UIElement.Opacity)

To =0/>





< trigger.exitactions> < beginstoryboard>

< storyboard>

< doubleanimation storyboard.targetname =ButtonOver>

Storyboard.TargetProperty =( UIElement.Opacity)

To =0/>

< doubleanimation storyboard.targetname =Button>

故事板.TargetProperty =(UIElement.Opacity)

To =1/>





< trigger property =IsCheckedvalue =True> < trigger.enteractions> < beginstoryboard>

< storyboard>

< doubleanimation storyboard.targetname =CollapsedVisualOver>

Storyboard.TargetProperty =( UIElement.Opacity)

To =0/>

< doubleanimation storyboard.targetname =CollapsedVisual>

故事板.TargetProperty =(UIElement.Opacity)

To =0/>





< trigger.exitactions> < beginstoryboard>

< storyboard>

< doubleanimation storyboard.targetname =CollapsedVisualOver>

Storyboard.TargetProperty =( UIElement.Opacity)

To =1/>

< doubleanimation storyboard.targetname =CollapsedVisual>

故事板.TargetProperty =(UIElement.Opacity)

To =1/>













< / Style>



< grid>

< ; grid.columndefinitions>

< columndefinition width =Auto> < columndefinition width =*>



< telerik:radtreeview grid.column =0xmlns:telerik =#unknown>

ExpanderStyle ={ StaticResource ExpanderStyle}

Loaded =RadTreeView_Loaded_1>

< telerik:radtreeviewitem dropposition =Insideheader =Role>

< telerik:radtreeview x:name =treeRoles>

DisplayMemberPath =Title

ItemsSource ={Binding RoleCollection}/>





< telerik:radtreeviewitem dropposition =Insideheader =User>

< telerik :radtreeview x:name =treeUsers>

DisplayMemberPath =用户名

ItemsSource ={Binding UserCollection}/>





< telerik:radtreeviewitem dropposition =Insideheader =Modules>

< telerik:radtreeview x:name =treeModules>

ItemTemplate ={StaticResource SubTree}

ItemsSource ={Binding ModuleCollection}/>






I develops a project in vb.net using wpf. in this project i only use the USER CONTROL but when i run my project the design is too bing, even i already assign a small value to the designheight and designwidth, the display is still too big.

can someone help me with this matters. thank you very much experts!!
i am still new in this wpf.

Thank you very much!

The Xaml Code look like this:

<usercontrol x:class="UserRoleTreeViewUC" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:userRoleManagement="clr-namespace:UserRoleManagement"
d:DataContext="{d:DesignInstance Type=userRoleManagement:UserRoleTreeViewVM,
IsDesignTimeCreatable=False}"
d:DesignHeight="259.271"
d:DesignWidth="514.135"
mc:Ignorable="d">
<usercontrol.resources>

<datatemplate x:key="TreeContent">
<textblock text="{Binding Title}">


<hierarchicaldatatemplate x:key="SubTree">
ItemTemplate="{StaticResource TreeContent}"
ItemsSource="{Binding SubModules}">
<textblock fontweight="Medium" text="{Binding Title}">


<Style x:Key="ExpanderStyle" TargetType="{x:Type ToggleButton}">
<setter property="IsEnabled" value="True">
<setter property="IsTabStop" value="False">
<setter property="Cursor" value="Hand">
<setter property="Template">
<setter.value>
<controltemplate targettype="{x:Type ToggleButton}">
<grid>
<grid x:name="ButtonOver">
Width="16"
Height="16"
Margin="0,4,0,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Opacity="0">
<rectangle width="Auto">
Height="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
RadiusX="3"
RadiusY="3"
Stroke="DeepPink">
<rectangle.fill>
<lineargradientbrush startpoint="0.5,0" endpoint="0.5,1"> <gradientstop offset="0" color="#FF00BCFF">
<gradientstop offset="1" color="#FF027BA6">
<gradientstop offset="0.5" color="#FF00A5E0">
<gradientstop offset="0.501" color="#FF005674">


<rectangle x:name="CollapsedVisualOver">
Width="2"
Height="8"
Margin="7,4,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="#FFFFFFFF"
RadiusX="0"
RadiusY="0" />
<rectangle width="8">
Height="2"
Margin="4,7,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="#FFFFFFFF"
RadiusX="0"
RadiusY="0" />

<grid x:name="Button">
Width="16"
Height="16"
Margin="0,4,0,0"
HorizontalAlignment="Right"
VerticalAlignment="Top">
<rectangle width="Auto">
Height="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Fill="#FF00A2DC"
RadiusX="3"
RadiusY="3"
Stroke="#FF027BA6" />
<rectangle x:name="CollapsedVisual">
Width="2"
Height="8"
Margin="7,4,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="#FFFFFFFF"
RadiusX="0"
RadiusY="0" />
<rectangle width="8">
Height="2"
Margin="4,7,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="#FFFFFFFF"
RadiusX="0"
RadiusY="0" />



<controltemplate.triggers>
<trigger property="IsMouseOver" value="True"> <trigger.enteractions> <beginstoryboard>
<storyboard>
<doubleanimation storyboard.targetname="ButtonOver">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="1" />
<doubleanimation storyboard.targetname="Button">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="0" />


<trigger.exitactions> <beginstoryboard>
<storyboard>
<doubleanimation storyboard.targetname="ButtonOver">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="0" />
<doubleanimation storyboard.targetname="Button">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="1" />


<trigger property="IsChecked" value="True"> <trigger.enteractions> <beginstoryboard>
<storyboard>
<doubleanimation storyboard.targetname="CollapsedVisualOver">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="0" />
<doubleanimation storyboard.targetname="CollapsedVisual">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="0" />


<trigger.exitactions> <beginstoryboard>
<storyboard>
<doubleanimation storyboard.targetname="CollapsedVisualOver">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="1" />
<doubleanimation storyboard.targetname="CollapsedVisual">
Storyboard.TargetProperty="(UIElement.Opacity)"
To="1" />






</Style>

<grid>
<grid.columndefinitions>
<columndefinition width="Auto"> <columndefinition width="*">

<telerik:radtreeview grid.column="0" xmlns:telerik="#unknown">
ExpanderStyle="{StaticResource ExpanderStyle}"
Loaded="RadTreeView_Loaded_1 ">
<telerik:radtreeviewitem dropposition="Inside" header="Role">
<telerik:radtreeview x:name="treeRoles">
DisplayMemberPath="Title"
ItemsSource="{Binding RoleCollection}" />


<telerik:radtreeviewitem dropposition="Inside" header="User">
<telerik:radtreeview x:name="treeUsers">
DisplayMemberPath="Username"
ItemsSource="{Binding UserCollection}" />


<telerik:radtreeviewitem dropposition="Inside" header="Modules">
<telerik:radtreeview x:name="treeModules">
ItemTemplate="{StaticResource SubTree}"
ItemsSource="{Binding ModuleCollection}" />



推荐答案

WPF的设计不是那么原始(并且最终不是那么不可靠和不方便),只允许你设置控制的大小。所有渲染的大小都由窗口的总体布局定义。



首先,一切都来自窗户的尺寸,也可以是水平和垂直尺寸。它可以是窗口内所有UI元素的大小,由窗口的总大小定义,明确选择,或者窗口大小由内容大小定义。这由 SizeToContent 属性定义: https://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent(v = vs.110).aspx [ ^ ]。



然后,每个控件的大小由其内容和属性定义,例如 Control.Padding FrameworkElement.Margin ,以及相关因素,例如 Grid.ColumnDefinitions ,以及其他控件的属性。请参阅:

https://msdn.microsoft.com/en-us/library/system.windows.controls.control.padding%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.margin%28v=vs.110%29.aspx [ ^ ],

https:// msdn.microsoft.com/en-us/library/system.windows.controls.grid.columndefinitions(v=vs.110).aspx [<啊ref =https://msdn.microsoft.com/en-us/library/system.windows.controls.grid.columndefinitions(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.controls.grid.rowdefinitions(v = vs.110).aspx [ ^ ]。



我引用这些属性只是为了说明,而不是假装完整的解释。如果你研究WPF 布局模型,你可以得到这种理解: https://msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx [ ^ ]。



那么,该怎么办?重新思考你的设计。您不需要尝试任意更改某些单独控件的大小。可能WPF布局模型比你想象的更合理。最好是接受而不是打架。



-SA
WPF is not designed so primitively (and, ultimately, not so unreliably and inconveniently) to allow you to just set the size of control. All rendered sizes are defined by the general layout of the window.

First of all, everything comes from or to the size of the windows, separately by horizontal and vertical dimension. It's either the sizes of all UI elements inside a window are defined by the total size of the window, explicitly chosen, or the size of the window is defined by the size of content. This is defined by the SizeToContent property: https://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent(v=vs.110).aspx[^].

And then, the size of each and every control is defined by its content and properties, such as Control.Padding and FrameworkElement.Margin, as well as related factors, such as Grid.ColumnDefinitions and , and other controls' properties. Please see:
https://msdn.microsoft.com/en-us/library/system.windows.controls.control.padding%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.margin%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.grid.columndefinitions(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.grid.rowdefinitions(v=vs.110).aspx[^].

I referenced these properties just for illustration, not pretending for complete explanation. You can get this understanding if you study WPF layout model: https://msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx[^].

So, what to do? Rethink your design. You don't need to try to arbitrary change the size of some separate control. Probably WPF layout model is more rational that you thought. It's better to embrace in instead of fighting it.

—SA


这篇关于如何在运行时设置用户控件大小在Wpf中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆