【问题标题】:Late Binding, or accessing a page element from its resource dictionary后期绑定,或从其资源字典访问页面元素
【发布时间】:2017-01-18 01:05:15
【问题描述】:

作为一个简单的例子,我在资源字典中有一个按钮,我将把它存储在 ContentControl 中。我需要将 Button 的 Visibility 属性绑定到位于页面上的复选框,但该按钮是在 复选框之前创建的,因此我的设置器将不起作用。

有没有办法在页面初始化后进行绑定绑定?我知道我可以从后面的代码中做到这一点,但是我会有很多按钮,而且看起来相当将按钮的部分初始化代码放在不同的位置很麻烦。

<ResourceDictionary>
<button  x:Key = "MyButton">Hi There
<button.Style>
    <Style>
        <Setter Property="IsVisible" Value="false"/>
        <DataTrigger     // myCheckBox doesn't exist yet...
          Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True">
            <Setter Property="IsVisible" Value="true"/>
        <DataTrigger/>
    </Style>
</button.Style>
</button>
</ResourceDictionary>

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="1*"/>
    <RowDefinition Height="1*"/>
    <RowDefinition Height="1*"/>
 </Grid.RowDefinitions>

    <CheckBox x:Name = "myCheckBox" Row=1/> //This is made too late to bind my button to

    <ContentControl Content = "{StaticResource MyButton}" Row=2/>

</Grid>

我发现了类似lazy loading 的东西,你可以在需要时加载对象,我也探索过making my own binding class,但我只是不知道该去哪里。

我目前最喜欢的想法是这样的:

xaml:

property="{lateBinding source=whatever path=you.want}"

还有一些通用的 c# 类代码:

class lateBinding : Binding
{
    OnPageInitialized()
    {
        SetBinding(myObject, myProperty, myBinding);
    }
}

有什么想法吗?

【问题讨论】:

  • 我试过你的代码,它工作正常(除了你的代码有编译问题必须修复)。当你尝试运行它时会发生什么?

标签: c# wpf data-binding resourcedictionary late-binding


【解决方案1】:

您的代码只需稍作改动即可工作

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <Button  x:Key = "MyButton">Hi There
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Style.Triggers>
                            <DataTrigger  Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="False">
                                <Setter Property="Visibility" Value="Collapsed"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <CheckBox x:Name = "myCheckBox" Grid.Row="1"/> 

        <ContentControl Content = "{StaticResource MyButton}" Grid.Row="2"/>

    </Grid>
</Window>

【讨论】:

  • 事实证明我把我的问题简化了很多,所以我又问了一个问题here。但这绝对是我提出的问题的一个很好的答案。它帮助我朝着正确的方向前进。谢谢!
猜你喜欢
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 2013-01-28
  • 2011-07-21
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多