ItemsControl中按钮的内容绑定到List不更新

时间:2017-11-08 16:35:54

标签: c# wpf mvvm

我的视图中有一个按钮:

<Button Style="{StaticResource DeleteButton}" Content="{Binding toDelete, Converter={StaticResource DeleteButtonContentConverter}}" Command="{Binding DeleteCommand}" Grid.Row="0" Grid.Column="4"/>

当删除更改时,不会更新。我知道我的转换器工作正常,因为文本在首次加载视图时正确显示(当我对其进行硬编码以确保它可以双向工作时)。我假设GUI正在通知列表更改,因为我在日志中记录了它,但我不确定如何直接测试通知:

    private List<AppList> _appList;
    public List<AppList> appList
    {
        get { return _appList; }
        set { Set(ref _appList, value); Log.Debug("appList changed"); }
    }

我认为问题是由于按钮是绑定到ItemsControl的列表的一部分引起的,但我无法弄清楚如何更新List中对象的属性。

作为测试,我创建了一个事件,当单击按钮时触发该事件(单击应更改按钮本身的内容)并尝试使用RaisePropertyChanged通知对List和绑定到Content的属性的更改本身。两者都没有。

如果它有助于ItemsControl的WPF:

    <ScrollViewer Style="{StaticResource ScrollviewerStyle}" Grid.Row="2">
        <ItemsControl ItemsSource="{Binding appList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Style="{StaticResource ItemsControlBorderStyle}">
                        <Grid Width="540">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition Width="255"/>
                                <ColumnDefinition Width="70"/>
                            </Grid.ColumnDefinitions>
                            <Label Style="{StaticResource LabelStyle}" Content="Alias:" Grid.Row="0" Grid.Column="0"/>
                            <TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding alias, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Grid.Row="0" Grid.Column="1"/>
                            <Label Style="{StaticResource LabelStyle}" Content="Path:" Grid.Row="0" Grid.Column="2"/>
                            <TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding path, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="255" Grid.Row="0" Grid.Column="3"/>
                            <Button Style="{StaticResource DeleteButton}" Content="{Binding toDelete, Converter={StaticResource DeleteButtonContentConverter}}" Command="{Binding DeleteCommand}" Grid.Row="0" Grid.Column="4"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

1 个答案:

答案 0 :(得分:1)

AppList类应实现INotifyPropertyChanged接口,并在PropertyChanged属性设置为新值时引发toDelete事件。

然后每次设置属性时都应调用转换器,并且Content的{​​{1}}应设置为转换器返回的任何内容。