xaml 中声明的绑定不起作用
我有一个包含网格的主窗口,在窗口加载事件期间,我将动态创建用户控件的实例并将其添加到网格中。为了让用户控件在主窗口调整大小时能够自适应,我想将用户控件的宽度和高度绑定到网格的 ActualWidth
和 ActualHeight
上。
第一种方法是在代码中创建绑定对象,在 window_loaded 事件中的同一位置,
Binding widthBinding = new Binding("ActualWidth");
widthBinding.Source = panel;
BindingOperations.SetBinding(uc, WidthProperty, widthBinding);
Binding heightBinding = new Binding("ActualHeight");
heightBinding.Source = panel;
BindingOperations.SetBinding(uc, HeightProperty, heightBinding);
panel.Children.Add(uc);
它按预期工作。
第二种方法是在用户控件的xaml文件中使用xaml绑定,
<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded"
Width="{Binding ElementName=ContainerElement, Path=ActualWidth}"
Height="{Binding ElementName=ContainerElement, Path=ActualHeight}">
或者
<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualHeight}">
但是这不起作用。
我可以知道 xaml 方法有什么问题吗?
I have a main window which contains a grid, during the window loaded event, I will dynamically create an instance of a user control and add it to grid. In order to let the user control to adapt itself when the main window is resized, I want to bind the user control's width and height to the grid's ActualWidth
and ActualHeight
.
The first way is to create the binding object in code,same place in the window_loaded event,
Binding widthBinding = new Binding("ActualWidth");
widthBinding.Source = panel;
BindingOperations.SetBinding(uc, WidthProperty, widthBinding);
Binding heightBinding = new Binding("ActualHeight");
heightBinding.Source = panel;
BindingOperations.SetBinding(uc, HeightProperty, heightBinding);
panel.Children.Add(uc);
it worked as expected.
The second way is to use xaml binding in the user control's xaml file,
<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded"
Width="{Binding ElementName=ContainerElement, Path=ActualWidth}"
Height="{Binding ElementName=ContainerElement, Path=ActualHeight}">
or
<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualHeight}">
But this did not work.
May I know what is wrong with the xaml approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用对齐而不是绑定吗?
绑定的问题是,如果面板中的某些内容导致
ActualHeight
和ActualWidth
增加,则可能会增加。对于 StackPanel 来说尤其如此。如果您使用
Grid
,它可能会与绑定到父ActualWidth
和ActualHeight
一起使用。我发现有时它可以工作,但通常面板中的某些东西会使尺寸增加并弄乱装订。Can you try using the alignments instead of binding?
The problem with binding is that the
ActualHeight
andActualWidth
might increase if something in the panel makes it increase. This is especially true withStackPanel
s.If you arr using a
Grid
, it might work with binding to the parentActualWidth
andActualHeight
. I have found that sometimes it works, but often something in the panel makes the size increase and messes up the binding.