Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在更新wpf中现有项的同时向itemscontrol添加新项_C#_Wpf - Fatal编程技术网

C# 如何在更新wpf中现有项的同时向itemscontrol添加新项

C# 如何在更新wpf中现有项的同时向itemscontrol添加新项,c#,wpf,C#,Wpf,很难解释我的问题,但我会试一试 我有一个ItemsControl,其中有一个ItemTemplate按钮,单击该按钮后,我必须向ItemsControl添加新项目。最后一行应该只有按钮 问题 最初在绑定期间,我可以隐藏行的按钮(最后一个除外),但在单击按钮并添加动态行时,我无法隐藏上一个按钮 希望我已经解释了我的问题。如果不够清楚,请告诉我。我无法解决这个问题 这是我的实现 我的主窗口 <Window x:Class="ObservableCollectionUpdation.MainWi

很难解释我的问题,但我会试一试

我有一个ItemsControl,其中有一个ItemTemplate按钮,单击该按钮后,我必须向ItemsControl添加新项目。最后一行应该只有按钮

问题 最初在绑定期间,我可以隐藏行的按钮(最后一个除外),但在单击按钮并添加动态行时,我无法隐藏上一个按钮

希望我已经解释了我的问题。如果不够清楚,请告诉我。我无法解决这个问题

这是我的实现

我的主窗口

<Window x:Class="ObservableCollectionUpdation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ObservableCollectionUpdation"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <local:IsLastItemConverter x:Key="LastItemConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>

        <ItemsControl x:Name="RecordsControl" ItemsSource="{Binding Records}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150"/>
                            <ColumnDefinition Width="150"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="{Binding Name}"></TextBlock>
                        <TextBlock Grid.Column="1" Text="{Binding TotalCount}"></TextBlock>
                        <Button Grid.Column="2" Content="Add" Command="{Binding ElementName=RecordsControl, Path= DataContext.AddCommand}"
                                Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                Converter={StaticResource LastItemConverter}}"></Button>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

问题是,转换器是为每一行单独调用的,当对该行求值时,这是最后一行,因此所有行都是可见的


有几种方法可以处理这个问题,最简单的方法是,将新项目作为属性,并且只有当新项目更改更新可见性时,我不知道这是否是有效的解决方案,但下面是我找到的解决方案。也就是说,将集合重新绑定到控件,因此更改DoAdd方法以重新创建
ObservableCollection
解决了该问题

        private void DoAdd()
        {
            Records.Add( new Record
            {
                Name = "Added",
                TotalCount = Records.Count + 1
            });

            Records = new ObservableCollection<Record>(Records);
            OnPropertyChanged("Records");
        }
private void DoAdd()
{
记录。添加(新记录)
{
Name=“已添加”,
TotalCount=记录。计数+1
});
记录=新的可观察收集(记录);
不动产变更(“记录”);
}

为什么不重新评估每一行,它本身的集合将正确更改。我怎样才能解决它@MudsQuite没有获得,但我正在向集合中添加新项目,但没有正确更新它@MudsQuite您需要对最后一行项目的引用,然后您可以使用它的可见性。。因此,在视图模型中将最后添加的项作为属性,可能是您也可以将其绑定到所选项,然后更改所选项样式。为什么不在itemscontrol之外添加一个Add按钮?是的,这就是我要做的。这与我想要控制itemscontrol的按钮无关。按钮只是我的POC中的一个示例@如果不需要控制项控件,只需控制viewmodel中的集合即可。itemscontrol仅用于显示您的收藏。尽管如此,一个简单的方法是向记录对象添加一个新属性(IsLastItem),这样您就可以绑定到它,并且可以在add命令中轻松设置该属性。所以每次添加新记录时,我都必须遍历集合并更新IsLastItem属性@盲眼
public class IsLastItemConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            DependencyObject item = (DependencyObject)value;
            ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);

            return (ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType,
                                  object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
        private void DoAdd()
        {
            Records.Add( new Record
            {
                Name = "Added",
                TotalCount = Records.Count + 1
            });

            Records = new ObservableCollection<Record>(Records);
            OnPropertyChanged("Records");
        }