我有一个Windows,其中包含两个列表框和一个按钮。 这两个列表框数据绑定到类“ ObjectList”的列表,并在其中包含ObservableCollection。 First ListBox(我们将其称为“ ObjectLists”)在其中包含文本框。 第二个ListBox(我们将其称为“ ObjectListsSelected”)在其中包含Listbox。 我使用那些顶部的列表框来获取动态UI。
按钮执行一个命令,该命令用于拆分第一个列表框中的文本(按照\ r \ n个字符),将其添加到第二个列表框中,然后清除第一个列表框中的文本框。
“ ObjectLists”的数据绑定出现问题。 尽管该属性获得了此更新,但Clean选项不会反映在UI中。
在调试此方案时,我发现“清除”操作没有进入属性集。 我当然使用了物业变更,但没有任何效果。
主窗口
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:ListToTextConverter x:Key="converter1"></local:ListToTextConverter>
</Window.Resources>
<Grid Name="ObjPanel">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Object" Height="30"/>
<ListBox Name="ObjectLists" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding OB1,Mode=TwoWay}" BorderThickness="0" Background="Transparent" Width="200" HorizontalAlignment="Center">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Name="ObjectList" Text="{Binding content,Mode=TwoWay,Converter={StaticResource converter1}}" Margin="0,0,30,0" Height="225" Width="200" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="AddButton" Command="{Binding AddCommand}" Grid.Row="1" Grid.Column="1" Height="50" Width="150" HorizontalAlignment="Center" Margin="0,0,10,0">Add</Button>
<Label Content="ObjectSelect" Height="30" Grid.Column="2"/>
<!--<ListBox Name="ObjectListSelectedBox1" ItemsSource="{Binding ObjSelected}" Grid.Row="1" Grid.Column="3" Width="200" Height="225" HorizontalAlignment="Center"/>-->
<ListBox Name="ObjectListsSelected" Grid.Row="1" Grid.Column="2" ItemsSource="{Binding OB2,Mode=TwoWay}" BorderThickness="0" Background="Transparent" Width="200" HorizontalAlignment="Center">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<ListBox Name="ObjectListSelected" ItemsSource="{Binding content,Mode=TwoWay}" Margin="0,0,30,0" Height="225" Width="200" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
类ObjectList
class ObjectList
{
public string header { get; set; }
private ObservableCollection<string> _content;
public ObservableCollection<string> content
{
get { return _content; }
set
{
_content = value;
}
}
public ObjectList()
{
content=new ObservableCollection<string>();
}
}
主视图模型
MainVM类:INotifyPropertyChanged { 公共RelayCommand AddCommand {组; }
public List<ObjectList> OB2 { get; set; }
private List<ObjectList> ob1;
public List<ObjectList> OB1
{
get { return ob1; }
set {
ob1 = value;
OnPropertyChanged(nameof(ob1));
}
}
public MainVM()
{
this.AddCommand = new RelayCommand(Add);
OB1 = new List<ObjectList>()
{
new ObjectList()
};
OB2 = new List<ObjectList>()
{
new ObjectList()
};
}
public void Add()
{
foreach (var s in OB1[0].content)
{
OB2[0].content.Add(s);
}
OB1[0].content.Clear();
OnPropertyChanged(nameof(OB1));
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
封面
public class ListToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
StringBuilder sb = new StringBuilder();
Type TEST = value.GetType();
foreach (string s in (ObservableCollection<string>)value) sb.AppendLine(s);
string str = sb.ToString();
return str;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string[] lines = Regex.Split((string)value, "\r\n");
ObservableCollection<string> OC = new ObservableCollection<string>(lines);
return (OC);
}
}