带有DataTemplate的ListBox识别SelectedItem

时间:2017-07-31 18:40:15

标签: c# .net-core avaloniaui

我的ListBox有一个简单的DataTemplate,一个CheckBox和一个TextBox。 如果用户检查CheckBox我想要获取此更改的项目,例如SelectedItem的属性ListBox

如何从List2获取已更改的元素?

MyListItem

public class MyListItem2 : ReactiveObject
{

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            this.RaiseAndSetIfChanged(ref _name, value, "Name");
        }
    }
    private bool _isMarked;
    public bool IsMarked
    {
        get { return _isMarked; }
        set
        {
            this.RaiseAndSetIfChanged(ref _isMarked, value, "IsMarked");
        }
    }
}

查看:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataTemplate.Views.MainWindow"
        xmlns:viewsmodels="clr-namespace:DataTemplate.ViewModels;assembly=DataTemplate"
        xmlns:dt="clr-namespace:DataTemplate;assembly=DataTemplate"
        Title="DataTemplate" Width="700">
<Window.DataContext>
  <viewsmodels:MainWindowViewModel />
</Window.DataContext>

<Grid ColumnDefinitions="250">
  <ListBox Grid.Column="1" Items="{Binding List2}">
    <ListBox.ItemTemplate>
      <DataTemplate DataType="dt:MyListItem2">
        <Grid ColumnDefinitions="50*,50*">              
          <CheckBox Grid.Column="0" Content="Mark" IsChecked="{Binding IsMarked}"/>  
          <TextBox Grid.Column="1" Text="{Binding Name}"/>  
        </Grid>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
</Grid>

视图模型:

public class MainWindowViewModel : ReactiveObject
{
    public ObservableCollection<MyListItem2> List2 { get; set; }    

    public MainWindowViewModel()
    {
        List2 = new ObservableCollection<MyListItem2>();
        Random rand = new Random();

        for (int i = 0; i < rand.Next(1, 20); i++)
        {
            MyListItem2 mli = new MyListItem2();
            mli.Name = "ListItem" + i;
            mli.IsMarked = false;
            mli.PropertyChanged += ItemChanged; 
            List2.Add(mli);
        }
    }
    private void ItemChanged(object sender, PropertyChangedEventArgs e)
    {            
        var item = sender as MyListItem2;
        Console.WriteLine(string.Format("changed: {0} {1}", item.Name, item.IsMarked));            
    }
}

3 个答案:

答案 0 :(得分:3)

我可以看到两种方式:

  • 由于您使用的是MVVM,请在MyListItem2类(Microsoft Reference on INotifyPropertyChanged implementation)上实现INotifyPropertyChanged接口。设置/更改IsMarked值时引发属性更改事件,然后连接到项的PropertyChanged事件处理程序以确定何时更改它。 。 OR
  • 如果您有codebehidn,请在XAML的复选框本身上添加“Checked”和/或“Unchecked”事件处理程序。如下所示。

    CheckBox Grid.Column =“0”Content =“Mark”IsChecked =“{Binding IsMarked}”/&gt;
          Checked =“IsMarked_Checked”

代码隐藏

public void IsMarked_Checked(object sender, RoutedEventArgs e)
{
    var ck = sender As Checkbox;

    if (ck == null) 
    {
         return;
    }

    // do whatever you need to here using the datacontext of the Checkbox
}

答案 1 :(得分:0)

如果您想知道用户选中/取消选中复选框的时间,您需要从复选框中触发事件。

使用类似的东西:

private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
    //check IsChecked of MyCheckBox here
}

答案 2 :(得分:0)

尝试设置绑定模式:

SelectedItem="{Binding SelectedItem, Mode=TwoWay}"