2

我有ListBox一个简单DataTemplate的 aCheckBox和 a 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));            
    }
}
4

3 回答 3

3

我可以看到两种方式:

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

    CheckBox Grid.Column="0" Content="Mark" IsChecked="{Binding IsMarked}"/>
    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
}
于 2017-07-31T18:47:05.987 回答
0

尝试设置绑定模式:

SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
于 2017-08-01T21:10:28.737 回答
0

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

使用这样的东西:

private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
    //check IsChecked of MyCheckBox here
}
于 2017-07-31T18:46:45.373 回答