【问题标题】:Caliburn Micro bind property to method with parametersCaliburn Micro 将属性绑定到带参数的方法
【发布时间】:2016-06-27 03:25:43
【问题描述】:

我在我的应用程序中使用 Caliburn.Micro。 我有这个列表框

<ListBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Source={x:Static models:Tags.AvailableTags}}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <CheckBox Content="{Binding Name}" IsChecked="{Binding ???}"/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ListBox>

基本上,我想将 CheckBox 的 IsChecked 属性绑定到 DataContext 的方法。我该怎么做呢?我知道如何在 Caliburn.Micro 中绑定事件,但我从未将方法绑定到属性。该方法还有一个属性。

【问题讨论】:

  • 我的大脑快要炸了)))您不能将属性绑定到方法。更准确地说,你想达到什么目的?)))

标签: c# wpf mvvm data-binding caliburn.micro


【解决方案1】:

您应该将视图中的 ListBox 集合绑定到具有您希望显示的属性的 BindableCollection,即 Name 和 Enabled。

public class MyCollection
{ 
    public string Name { get; set; }
    public bool Checked { get; set; }
}

然后在你的视图模型中绑定到 MyCollection 对象的集合:

public class YourViewModel : Screen
{
    private readonly IEventAggregator _Aggregator;
    private BindableCollection<MyCollection> tags= new BindableCollection<MyCollection>();

    public YourViewModel(IEventAggregator aggregator)
    {
        if (aggregator == null)
            throw new ArgumentNullException("aggregator");
        _Aggregator = aggregator;
        _Aggregator.Subscribe(this);
    }

    public BindableCollection<MyCollection> AvailableTags
    {
        get
        {
            if (tags== null)
                tags= new BindableCollection<MyCollection>();

            return tags;
        }
        set
        {
            tags= value;
            NotifyOfPropertyChange(() => MyCollection);
        }
    }  
}

由于您还使用标准 WPF ListBox,Caliburn 允许您根据约定将集合绑定到您的视图:&lt;ListBox x:Name=AvailableTags /&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2015-01-20
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多