每个ListBox项内的注释部分

时间:2016-05-24 14:39:29

标签: c# wpf data-binding listbox

我正在使用C#/ WPF开发一个外部应用程序,它显示信息:NameDetails。等...

我用ListBox和简单的Binding来处理这个部分。我用这种方式填充我的ListBox:

 model.ClassAs.Add(new ClassA { Name = textBox1.Text, Detail = textBox2.Text });

现在出现问题:我现在必须为用户实施Comment Section,以便在每个ListBoxItems内分别添加评论。显然,当我填充Comment SectionName时,我无法填写Details。所以我在第一个ItemsTemplate内创建第二个ListBox,如果需要,可以创建一个ListBoxSeption。我创建了另一个类ClassB并尝试像第一次一样实现Binding。

显然这不起作用,因为我在ListBox1的DataContext中。我尝试使用ObservableCollection事件设置PropertyChanged,但我无法让它工作。我不太了解这个过程所以我不知道我的错误在哪里。

这是我的XAML:

<ListBox x:Name="listBox" MouseDoubleClick="ListBox_MouseDoubleClick"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch"
             ItemsSource="{Binding ClassAs, Mode=OneWay}" Grid.Column="1" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="grd">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <TextBlock Name="Name" Text="{Binding Name, Mode=OneWay}" Grid.Column="2" Foreground="DarkGray" FontWeight="Bold" />
                <TextBlock Text="{Binding Detail, Mode=OneWay}" Grid.Column="2" Grid.Row="1" TextWrapping="Wrap" />
                <Grid x:Name="comSection" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected, Converter={StaticResource booleanVisibleConverter}}"
                      Grid.Column="2" Grid.Row="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <AdornerDecorator>
                        <TextBox x:Name="textBoxCom" HorizontalAlignment="Stretch"
                    TextWrapping="Wrap" Grid.Column="0" Grid.Row="0">
                            <controls:WatermarkService.Watermark>
                                <TextBlock>Type Comment Here...</TextBlock>
                            </controls:WatermarkService.Watermark>
                        </TextBox>
                    </AdornerDecorator>
                    <ListBox x:Name="listBoxCom" BorderThickness="0,0,0,0" Grid.Row="1" Grid.Column="0"
                             HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Visible"
                             ItemsSource="{Binding ClassBs.Commentary}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="20"/>
                                        <RowDefinition Height="20"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Name="TBCom" Text="{Binding}"></TextBlock>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
                <Button x:Name="addComment" HorizontalAlignment="Center" VerticalAlignment="Top" Width="25" Height="25"
                        BorderThickness="0" Click="addComment_Click" Background="Transparent" Grid.Column="1" Grid.Row="2"
                        ToolTip="Comment">
                    <Button.Content>
                        <Image Source="Assets\plus_orange.png" />
                    </Button.Content>
                </Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我想要的是:用户填写textBoxCom并按addComment按钮在所选的ListBoxItem中添加注释。

我的课程:

public sealed class ClassA : INotifyPropertyChanged
{
    public string Name { get; set; }
    public string Detail { get; set; }

    public ObservableCollection<ClassB> ClassBs { get; set; }
    public List<ClassB> ModifiedComments { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public class ClassB
    {
        string Commentary { get; set;}
    }
}

我的ViewModel:

public sealed class ViewModel
{
    public ObservableCollection<ClassA> ClassAs { get; set; }
    public ObservableCollection<ClassA.ClassB> ClassBs { get; set; }

    List<ClassA.ClassB> ModifiedItems { get; set; }

    public ViewModel()
    {
        ClassAs = new ObservableCollection<ClassA>();
        CLassBs = new ObservableCollection<ClassA.ClassB>();
        ModifiedItems = new List<ClassA.ClassB>();
        this.ClassBs.CollectionChanged += this.OnCollectionChanged;
    }

    void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        try
        {
            if (e.NewItems != null)
            {
                foreach (ClassA.ClassB newItem in e.NewItems)
                {
                    ModifiedItems.Add(newItem);

                    //Add listener for each item on PropertyChanged event
                    newItem.PropertyChanged += this.OnItemPropertyChanged;
                }
            }

            if (e.OldItems != null)
            {
                foreach (ClassA.ClassB oldItem in e.OldItems)
                {
                    ModifiedItems.Add(oldItem);

                    oldItem.PropertyChanged -= this.OnItemPropertyChanged;
                }
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show("me : " + ex);
        }
        }

    void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        ClassA.ClassB item = sender as ClassA.ClassB;
        if (item != null)
            ModifiedItems.Add(item);
    }
} 

我的点击事件(AddComment)

private void addComment_Click(object sender, RoutedEventArgs e)
{
    try
    {
        currentSelectedListBoxItem = this.listBox.ItemContainerGenerator.ContainerFromIndex(listBox.SelectedIndex) as ListBoxItem;
        System.Windows.Controls.TextBox textBoxCom = Helper.FindDescendant<System.Windows.Controls.TextBox>(currentSelectedListBoxItem);
        ListBox LBCom = Helper.FindDescendant<ListBox>(currentSelectedListBoxItem);
        if (string.IsNullOrWhiteSpace(textBoxCom.Text))
            MessageBox.Show("Please fill the Comment Section");
        else
        {
            model.ClassBs.Add(new ClassA.ClassB { Commentary = textBoxCom.Text });
            textBoxCom.Clear();

        }
    }
    catch (Exception exp)
    {
        MessageBox.Show("exp = " + exp);
    }
}

很抱歉很长的帖子,我希望你可以帮我解决这个问题!

1 个答案:

答案 0 :(得分:0)

这比预期的简单,我只是挖掘无用的代码! 我删除了有关ObservableCollectionPropertyChange事件的所有代码。

我的最终ClassA代码(不再是ClassB):

public sealed class ClassA
{


    public string Name { get; set; }
    public string Detail { get; set; }
    public List<string> Comments { get; set; }

    public Issue()
    {
        Comments = new List<string>();
    }
}

我的最终点击事件:

var selected = listBox.SelectedItem as ClassA;
                selected.Comments.Add(textBoxCom.Text);
                listBox.Items.Refresh();
                textBoxCom.Clear();

谢谢@ Sidewinder94!