xaml 中声明的绑定不起作用

发布于 2024-08-15 01:22:32 字数 1689 浏览 7 评论 0原文

我有一个包含网格的主窗口,在窗口加载事件期间,我将动态创建用户控件的实例并将其添加到网格中。为了让用户控件在主窗口调整大小时能够自适应,我想将用户控件的宽度和高度绑定到网格的 ActualWidthActualHeight 上。

第一种方法是在代码中创建绑定对象,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

才能让你更想念 2024-08-22 01:22:32

您可以尝试使用对齐而不是绑定吗?

<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" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

绑定的问题是,如果面板中的某些内容导致 ActualHeightActualWidth 增加,则可能会增加。对于 StackPanel 来说尤其如此。

如果您使用Grid,它可能会与绑定到父ActualWidthActualHeight一起使用。我发现有时它可以工作,但通常面板中的某些东西会使尺寸增加并弄乱装订。

Can you try using the alignments instead of binding?

<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" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

The problem with binding is that the ActualHeight and ActualWidth might increase if something in the panel makes it increase. This is especially true with StackPanels.

If you arr using a Grid, it might work with binding to the parent ActualWidth and ActualHeight. I have found that sometimes it works, but often something in the panel makes the size increase and messes up the binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文