Caliburn Micro将属性绑定到带参数

时间:2016-03-11 18:50:14

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

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

<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中的事件,但我从来没有将方法绑定到属性。该方法也有一个属性。

1 个答案:

答案 0 :(得分:0)

您应该将视图中的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允许您根据约定将视图绑定到视图:<ListBox x:Name=AvailableTags />

相关问题