簡體   English   中英

從UI更改ObservableCollection而不更新Bound Collection

[英]Changes to ObservableCollection from UI not updating the Bound Collection

在XAML中,我有一個列表視圖:

<UserControl x:Class="HspSetup.View.UserInputData"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:HspSetup.View"
             xmlns:viewModel="clr-namespace:HspSetup.ViewModel"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:Behaviours="clr-namespace:HspSetup.Behaviours"
             mc:Ignorable="d" 
             Height="Auto" Width="Auto">
    <UserControl.Resources>
        <viewModel:UserInputDataViewModel x:Key="ViewModel"/>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="0.1*"/>       
            <RowDefinition Height="Auto"/>      
            <RowDefinition Height="*"/>       
            <RowDefinition Height="*"/>    
            <RowDefinition Height="*"/>       
            <RowDefinition Height="*"/>      
            <RowDefinition Height="*"/>     
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*"   />
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="0.5*" />
        </Grid.ColumnDefinitions>

        <TextBlock  Background="Coral" Height="50"
                    VerticalAlignment="Center" TextAlignment="Center"
                    Grid.Row="0"  Grid.ColumnSpan="3">
            <TextBlock.Text>Some More Data</TextBlock.Text>
            <TextBlock.FontFamily>Segoe UI</TextBlock.FontFamily>
            <TextBlock.Foreground>White</TextBlock.Foreground>
            <TextBlock.FontSize>30</TextBlock.FontSize>
        </TextBlock>

        <ListView   Grid.Row="2" Width="Auto" 
                    Behaviours:GridViewColumnResize.Enabled="True"
                    Height="Auto" Name="OrderNumberListView" 
                    IsSynchronizedWithCurrentItem="True" Grid.Column="1"
                    DataContext="{Binding Source={StaticResource ViewModel}}" 
                    ItemsSource="{Binding ConfigObjects}" >
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" 
                            Value="Stretch" />
                    <Setter Property="Behaviours:LostFocus.LostFocusCommand"
                            Value="{Binding Path=LostFocusCommand, Source={StaticResource ViewModel}}"/>
                    <Setter Property="Behaviours:LostFocus.CommandParem" 
                            Value="{Binding}"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>

                    <GridViewColumn Header="Order Name" 
                                    Behaviours:GridViewColumnResize.Width="*">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding OrderNumber, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                         Style="{StaticResource flatTextBox}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Type Name"
                                    Behaviours:GridViewColumnResize.Width="*">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding TypeName, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                         Style="{StaticResource flatTextBox}">
                                </TextBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Behaviours:GridViewColumnResize.Width="*"  >
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <Button Content="Add" 
                                        Command="{Binding AddConfigObjectCommand, Mode=OneWay, Source={StaticResource ViewModel}}"/>
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumnHeader HorizontalContentAlignment="Stretch"/>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="RemoveButton" 
                                        Content="Remove" 
                                        Command="{Binding RemoveConfigObjectCommand, Mode=OneWay, Source={StaticResource ViewModel}}" CommandParameter="{Binding}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</UserControl>

對應的UI如下所示:

對應的UI看起來像這樣

單擊添加按鈕我添加一個空行,如下所示:

UI更新如下

我在文本框中輸入文本。 通過在文本框中輸入文本對列表視圖所做的更改不會更新文本框綁定到的observableCollection。

可觀察集合的類如下:

class UserInputDataViewModel: BaseViewModel
{       
    private ObservableCollection<ConfigObject> _configObjects;

    public ObservableCollection<ConfigObject> ConfigObjects
    {
        get { return _configObjects; }
        set
        {
            _configObjects = value;
            OnPropertyChanged("ConfigObjects");
        }
    }

    private ICommand _addConfigObject;

    public ICommand AddConfigObjectCommand
    {
        get
        {
            if (_addConfigObject == null)
            {
                _addConfigObject = new RelayCommand(
                    p => AddConfigObject(),
                    p => CanAddConfigObject);
            }
            return _addConfigObject;
        }
    }

    private void AddConfigObject()
    {
        ConfigObjects.Add(new ConfigObject() { OrderNumber = "", TypeName = "" });
        OnPropertyChanged("ConfigObjects");
    }

    private ICommand _removeConfigObject;

    public ICommand RemoveConfigObjectCommand
    {
        get
        {
            if (_removeConfigObject == null)
            {
                _removeConfigObject = new RelayCommand(
                    p => RemoveConfigObject((ConfigObject)p),
                    p => CanRemoveConfigObject);
            }
            return _removeConfigObject;
        }
    }

    private void RemoveConfigObject(ConfigObject configObject)
    {
        ConfigObjects.Remove(configObject);
        OnPropertyChanged("ConfigObjects");
    }

    bool CanAddConfigObject
    {
        get { return true; }
    }
    bool CanRemoveConfigObject
    {
        get { return true; }
    }
}

class BaseViewModel:INotifyPropertyChanged
{
    /// <summary>
    /// Name of the property that changed
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Send the notification to the property that change
    /// </summary>
    /// <param name="pPropertyName">name of the property</param>
    protected virtual void OnPropertyChanged(string pPropertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, 
                                 new PropertyChangedEventArgs(pPropertyName));
        }
    }
}

internal class ConfigObject : INotifyPropertyChanged
{
    private string _orderNumber;

    public string OrderNumber
    {
        get { return _orderNumber; }
        set { _orderNumber = value; OnPropertyChanged("OrderNumber"); }
    }

    private string _typeName;


    public string TypeName
    {
        get { return _typeName; }
        set { _typeName = value; OnPropertyChanged("TypeName"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string paramname)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(paramname));
    }
}

通過編輯文本框中的文本,您不會更改ObservableCollection但可以更改集合中的一個對象。 該集合不知道更改其中一個對象的內部(!)狀態。 ConfigObject類的PropertyChanged事件可以捕獲這些更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM