数据绑定到动态加载的UserControl

编程入门 行业动态 更新时间:2024-10-11 23:27:02
本文介绍了数据绑定到动态加载的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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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

本文发布于:2023-08-03 12:38:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   加载   动态   数据   UserControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!