数据绑定到动态加载的UserControl [英] DataBinding To Dynamically Loaded UserControl

查看:197
本文介绍了数据绑定到动态加载的UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我有使用另一个UserControls的窗口.这些控件会被一次加载和使用.加载的控件各不相同,但其中之一是通用的,它们具有名为IsValid的依赖项属性.我喜欢将该属性绑定到我的主窗口按钮.

我该如何实现?

这是我的问题的例子

Hi All!

I have Window which uses another UserControls. Those controls are loaded and used one in time. Loaded controls varies but one thing is commom, They have dependency property called IsValid. I like to bind that property to my main window button.

How do I achieve that?

Here is example of my problem

<window x:class="Example.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>       
                <!--Here we have dynamically loaded usercontrols. Control varies, but all have dependency property IsValid-->

                <ContentControl x:Name="DynamicContent"/>
                
                <!--Here Bind buttons IsEnabled to UserControl IsValid property-->                <Button Content="myButton" IsEnabled="???"></Button> 
       </StackPanel>
    </Grid>
</Window></window>



希望你有主意:)

干杯!



Hope you got idea :)

Cheers!

推荐答案

好像这个问题不太正确.按钮上的绑定应该绑定到视图模型上的属性(ContentValid或其他内容).您真正要问的问题是该属性应执行的操作.

您必须根据某些条件选择要包含的控件–希望在视图模型中.让所有控件实现一个接口(仅包含一个属性),让视图模型公开该类型的属性,即要显示的控件,然后ContentValid属性可以通过CurrentDynamicControl.IsValid查找该属性.

(该接口可能还需要公开UserControl,可以通过以下方式实现:
Seems like the question isn''t quite the right one. The binding on the button should be to a property on the view model (ContentValid or something). The question you really want to ask is what that property should do.

You must be choosing the control to include based on some criteria – hopefully in the view model. Have all the controls implement an interface (which includes just that one property), have the view model expose a property of that type which is the control to be shown, and the ContentValid property can then just look in the property by CurrentDynamicControl.IsValid.

(The interface may also need to expose a UserControl, which would be implemented thus:
public UserControl UserControl { get { return this; } }


...如果您想在视图模型上使用相同的属性来实际填充ContentControl,那可能是最好的方法.)


... if you want to use that same property on the view model to actually populate ContentControl, which would probably be for the best.)


这篇关于数据绑定到动态加载的UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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