我的研究完成了3天,没有任何效果。 好的,所以我可以将项目添加到Listview了! 如何从ObservableCollection的同一实例中删除selectedItem。
我需要在Listview的Xaml中获取SelectedItem吗?
如何在我的RemoveModPropList中引用SelectedItems并将其删除?
我的XAML
<Page.DataContext>
<m:Content/>
</Page.DataContext>
<Page.Resources>
<vm:VM_Base_ObsColl x:Key="VM_Base_ObsColl"/>
</Page.Resources>
<Grid>
<StackPanel
Background="DarkGray"
Height="500" Width="340"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="10,40,10,0">
<TextBox x:Name="boxInputCategory"
Header="Category"
Text="{Binding Category, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputTitle"
Header="Title"
Text="{Binding Title, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputMediaType"
Header="Media Type"
Text="{Binding MediaType, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputPlayTime"
Header="Play Time"
Text="{Binding PlayTime, Mode=TwoWay}"
Height="60" Width="250"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Lists"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="2 2"/>
<AppBarButton Icon="Add"
Content="Save All"
HorizontalAlignment="Right" Height="40"
Command="{Binding Path=Cmd_ADDModelPropsList,
Source={StaticResource VM_Base_ObsColl}}"
CommandParameter="{Binding}"/>
<AppBarButton Icon="Remove"
Content="Remove"
HorizontalAlignment="Right" Height="40"
Command="{Binding Path=Cmd_RemoveModPropsList,
Source={StaticResource VM_Base_ObsColl}}"
CommandParameter="{Binding}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="SQLite"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="2 2"/>
<AppBarButton Icon="Save"
Content="Save All"
HorizontalAlignment="Right"
Click="AppBarBtnAddText_Click"/>
</StackPanel>
<StackPanel Height="180">
<ListView x:Name="listViewOutput"
DataContext="{Binding Source={StaticResource VM_Base_ObsColl}}"
ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="70" Width="350" Margin="1 1" Background="Gray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Category}"
Grid.Column="0" Grid.Row="0"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding Title}"
Grid.Column="1" Grid.Row="0"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding MediaType}"
Grid.Column="0" Grid.Row="1"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding PlayTime}"
Grid.Column="1" Grid.Row="1"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</StackPanel>
</Grid>
型号
public class Content : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Content()
{
if (DesignMode.DesignModeEnabled)
{
Category = "Human Fx";
Title = "Out the Door CheckList";
MediaType = "Speech";
PlayTime = "10:00 AM";
}
}
private void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public override string ToString()
{
return Title + " ," + Category + " ," + MediaType + " " + PlayTime;
}
#region Class/Model/Record Props & Fields
public int Id { get; set; }
private string category;
public string Category
{
get => category;
set
{
category = value;
OnPropertyChanged("Category");
}
}
private string title;
public string Title
{
get => title;
set
{
title = value;
OnPropertyChanged("Title");
}
}
private string mediaType;//Speech,Audio,Video
public string MediaType
{
get => mediaType;
set
{
mediaType = value;
OnPropertyChanged("MediaType");
}
}
private string playTime;
public string PlayTime
{
get => playTime;
set
{
playTime = value;
OnPropertyChanged("PlayTime");
}
}
private string titleSet;
public string TitleSet
{
get => titleSet;
set
{
titleSet = value;
OnPropertyChanged("TitleSet");
}
}
private string playSet;
public string PlaySet
{
get => playSet;
set
{
playSet = value;
OnPropertyChanged("PlaySet");
}
}
private bool repeatsIsOn;
public bool RepeatsIsOn
{
get => repeatsIsOn;
set
{
repeatsIsOn = value;
OnPropertyChanged("RepeatsIsOn");
}
}
#endregion
}
ViewModel-在RemoveModPropsList方法中,我不知道如何从ListView中获取selectedItem,并从同一ObservableCollection实例中删除selected。
public class VM_Base_ObsColl : ObservableCollection<Content>
{
public Cmd_ADDModelPropsList Cmd_ADDModelPropsList { get; set; }
public Cmd_RemoveModPropsList Cmd_RemoveModPropsList { get; set; }
public VM_Base_ObsColl()
{
Cmd_ADDModelPropsList = new Cmd_ADDModelPropsList(this);
Cmd_RemoveModPropsList = new Cmd_RemoveModPropsList(this);
if (DesignMode.DesignModeEnabled)
{
for (int i = 1; i < 3; i++)
{
Add(new Content()
{
Category = "Category " + 1+i,
Title = "Title " + 1+i,
MediaType = "Media Type " + 1+i,
PlayTime = "8:00 AM " + 1 + i
});
}
}
}
public void ADDModelPropsList(Content content)
{
Content c = new Content();
c.Category = content.Category;
c.Title = content.Title;
c.MediaType = content.MediaType;
c.PlayTime= content.PlayTime;
Add(c);
}
public void RemoveModPropsList(Content content)
{
Content c = new Content();
c.Category = content.Category;
c.Title = content.Title;
c.MediaType = content.MediaType;
c.PlayTime = content.PlayTime;
Remove(c);
}
我的用于删除所选项目的ICommand文件
public class Cmd_RemoveModPropsList : ICommand
{
public VM_Base_ObsColl ViewModel { get; set; }
public Cmd_RemoveModPropsList(VM_Base_ObsColl viewModel)
{
ViewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
ViewModel.RemoveModPropsList(parameter as Content);
}
}
答案 0 :(得分:0)
似乎您正在创建一个新对象,并试图在调用from sklearn.tree import export_graphviz
from IPython import display
from sklearn.ensemble import RandomForestRegressor
m = RandomForestRegressor(n_estimators=1, max_depth=3, bootstrap=False, n_jobs=-1)
m.fit(X_train, y_train)
str_tree = export_graphviz(m,
out_file=None,
feature_names=X_train.columns, # column names
filled=True,
special_characters=True,
rotate=True,
precision=0.6)
display.display(str_tree)
时删除它。 RemoveModPropsList
将尝试删除此对象。但这不是集合的一部分。因此,不会删除任何内容。
解决方案是通过引用(ObservableColltection
的{{1}}属性)删除对象。然后,您可以将对象的特定实例传递给SelectedItem(s)
的{{1}}函数。
答案 1 :(得分:0)
您的问题有两种解决方案:
将与Add()相同的对象引用传递给Remove()
ObservableCollection<Class1> test = new ObservableCollection<Class1>();
Class1 obj = new Class1("Maria", 22203);
test.Add(obj);
test.Remove(obj); //This works
如果无法获得相同的参考,请使用linq。
ObservableCollection<Class1> test = new ObservableCollection<Class1>();
Class1 obj = new Class1("Maria", 22203);
test.Add(obj);
List<Class1> v = test.Where(x => x.Name == "Maria" && x.zip == 22203).ToList();
foreach (Class1 c in v)
{
test.Remove(c); //Also works
}