|ZYCWPF| WPF中的MVVM模式,我会Binding控件相应的属性,但是如何Binding控件的Children呢?谢谢

javamy018 2012-10-31 10:58:09
比如我要给窗体绑定标题,我可又在ViewModel中

private string title;
/// <summary>
/// 标题
/// </summary>
public string Title
{
get { return title; }
set
{
title = value;
this.RaisePropertyChanged("Title");
}
}

然后
Title="{Binding Title}"
来进行绑定

但是现在我要对XAML中的一个Canvas来动态添加他的控件
但是我发现在XAML中并没有<Canvas Children 属性可又给我绑定
我在后台定义了:public System.Windows.Controls.UIElementCollection CanvasChildren
那要怎么绑定给这个Canvas啊

谢谢
...全文
503 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jshi123 2012-10-31
  • 打赏
  • 举报
回复
霍霍
javamy018 2012-10-31
  • 打赏
  • 举报
回复
可以了。。引多一个System.Xaml

谢谢
javamy018 2012-10-31
  • 打赏
  • 举报
回复
编译不过提示:
类型“System.Windows.Markup.IQueryAmbient”在未被引用的程序集中定义。必须添加对程序集“System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。
javamy018 2012-10-31
  • 打赏
  • 举报
回复
Behavior
。。。
这东西怎么这么神奇。。。
jshi123 2012-10-31
  • 打赏
  • 举报
回复
先说下实现方法:
Canvas.Children本身是不能绑定的,我不知道有什么办法能实现绑定。
用behavior是可以的,方法还是绕个圈子在attach的时候把一组control加到cavas中。
看下面的例子:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Linq;

namespace WpfApp1
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}

Label _label = new Label {Content = "dynamic label"};
TextBox _text = new TextBox {Text = "dynamic text", Margin = new Thickness(150, 0, 0, 0 ) };
public UIElementCollection CanvasChildren
{
get { return new UIElementCollection(this, null) { _label, _text }; }
}
}

public class BindChildren : Behavior<Panel>
{
private static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof (UIElementCollection), typeof (BindChildren));

public UIElementCollection Children
{
get { return (UIElementCollection)GetValue(ChildrenProperty); }
set { SetValue(ChildrenProperty, value); }
}

protected override void OnAttached()
{
if (Children != null)
{
var children = Children.Cast<UIElement>().ToList();
Children.Clear(); // remove all children from original container in order to add it to new container
children.ForEach(child => this.AssociatedObject.Children.Add(child));
}
}
}
}


前台:

<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Canvas>
<i:Interaction.Behaviors>
<local:BindChildren Children="{Binding CanvasChildren}" />
</i:Interaction.Behaviors>
</Canvas>
</Window>


话说你的问题都挺偏门的。遇到这种比较奇怪的问题,我觉得你应该要考虑是不是思路有偏差。
binding一般都是用来绑定模型,而模型应该是数据,而不是UI元素,这样才有比较清楚的层次。
如果要动态添加控件,一般的做法是在方法里面调用control.Children.Add, 这样来添加,不必通过绑定来做。
lhx527099095 2012-10-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

C# code

public MainWindow()
{
InitializeComponent();
loginCtrl = new LoginControl(this);
this.loginCanvas.Children.Add(loginCtrl);
}


……
[/Quote]
这种属于纯ui逻辑 我一般直接放到codebehind的loaded里面
javamy018 2012-10-31
  • 打赏
  • 举报
回复
我尝试用:

<Canvas Name="loginCanvas" Grid.Column="1" Canvas.Children="{Binding canvasChildren}" Grid.Row="1" Width="500" Height="300" VerticalAlignment="Top" HorizontalAlignment="Center" >
</Canvas>

提示:
错误 1 属性“Children”没有可访问的资源库。
错误 2 “Canvas.Children”属性是只读的,且无法从标记设置

谢谢
javamy018 2012-10-31
  • 打赏
  • 举报
回复

public MainWindow()
{
InitializeComponent();
loginCtrl = new LoginControl(this);
this.loginCanvas.Children.Add(loginCtrl);
}

也就是如果不用MVVM的方法的话,是在窗体加载的时候
给loginCanvas里面添加一个用户控件
现在用MVVM的方法的话要怎么来实现呢?
lhx527099095 2012-10-31
  • 打赏
  • 举报
回复
具体需求放上来看看

如果说需要列表类型的话可以用itemscontrol控件做绑定
canvas 除非你自己写依赖属性 不然没法绑定内容
javamy018 2012-10-31
  • 打赏
  • 举报
回复
UP..........

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧