簡體   English   中英

問:C#WPF數據網格綁定到ObservableCollection <T> 沒有更新

[英]Q: C# WPF DataGrid bound to ObservableCollection<T> gets not updated

我目前在C#WPF應用程序中遇到一個問題,似乎很容易解決。 但是即使閱讀了幾篇Stackoverflow帖子,我仍然無法解決Web上的教程問題。

我有一個DataGrid,我正在嘗試綁定到ObservableCollection。 此外,我正在將MVVM模式(至少我正在嘗試)與PRISM一起使用。

我的問題是,在向ObservableCollection添加新規則后,不會填充DataGrid。 當我調試應用程序時,可以看到該集合中包含項。 從代碼中可以看到,我正在使用Bindablebase中的RaisePropertyChanged來觸發PropertyChangedEvent。 我也將DataGrid的ItemSource綁定到RuleList。

有人可以看到我在做什么嗎?

提前致謝,

麥可

這是我的RuleModel.cs:

namespace MyApp.GenericViews.Model
{
    public class RuleModel
    {
        public bool IsEnabled { get; set; }
        public string RuleName { get; set; }
        public string Source { get; set; }
        public string Target { get; set; }
        public string ModuleName { get; set; }
        public string ElementName { get; set; }

        public RuleModel()
        {

        }

    }
}

這是我的View.xaml:

<UserControl
    x:Class="MyApp.GenericViews.View.RulesView"
    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:local="clr-namespace:MyApp.GenericViews.View"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:MyApp.GenericViews.ViewModel"
    d:DataContext="{d:DesignInstance d:Type=vm:RulesViewModel}"
    d:DesignHeight="600"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Label Content="{Binding Title}" />
        <!--<StackPanel Grid.Row="0" Orientation="Horizontal">
            <Button Content="Add" Style="{StaticResource DefaultDialogButton}" />
            <Button Content="Remove" Style="{StaticResource DefaultDialogButton}" />
        </StackPanel>-->
        <DataGrid
            Name="RulesDataGrid"
            Grid.Row="1"
            Margin="5"
            AutoGenerateColumns="False"
            ItemsSource="{Binding Path=RuleList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
            <DataGrid.Columns>
                <!--<DataGridCheckBoxColumn Width="40" Binding="{Binding Path=IsEnabled}" />-->
                <DataGridTextColumn
                    Width="100"
                    Binding="{Binding Path=RuleName, UpdateSourceTrigger=PropertyChanged}"
                    Header="Name" />
                <DataGridTextColumn
                    Width="100"
                    Binding="{Binding ModuleName}"
                    Header="Module" />
                <!--<DataGridComboBoxColumn
                    Width="100"
                    Header="Element"
                    ItemsSource="{Binding ModuleList}"
                    SelectedItemBinding="{Binding Path=ElementName}" />-->
                <DataGridTextColumn
                    Width="50*"
                    Binding="{Binding Source}"
                    Header="Source" />
                <DataGridTextColumn
                    Width="50*"
                    Binding="{Binding Target}"
                    Header="Target" />
            </DataGrid.Columns>

        </DataGrid>
    </Grid>
</UserControl>

這是我的ViewModel.cs。

    namespace MyApp.GenericViews.ViewModel
{
    public class RulesViewModel : BindableBase, INavigationAware, IViewModel
    {
        private XmlDatabase _xmlDatabase;
        private readonly IUnityContainer _container;
        private readonly IEventAggregator _eventAggregator;

        public IMyAppView View { get; set; }

        public List<string> ModuleList { get; set; }

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    SetProperty(ref _title, value);
                    RaisePropertyChanged(nameof(Title)); 
                }
            }
        }


        private Rule _selectedRule;
        public Rule SelectedRule
        {
            get { return _selectedRule; }
            set
            {
                if (_selectedRule != value)
                {
                    SetProperty(ref _selectedRule, value);
                    RaisePropertyChanged(nameof(SelectedRule)); 
                }
            }
        }


        private ObservableCollection<RuleModel> _RuleList;

        public ObservableCollection<RuleModel> RuleList
        {
            get { return _RuleList; }
            set
            {
                if (_RuleList != value)
                {
                    SetProperty(ref _RuleList, value, nameof(RuleList));
                    //RaisePropertyChanged(nameof(RuleList));

                    //OnPropertyChanged(nameof(RuleList)); 
                }
            }
        }

        public RulesViewModel(IRuleView view, IUnityContainer c, IEventAggregator evt)
        {
            View = view;
            View.ViewModel = this;

            _container = c;
            _eventAggregator = evt;

            _eventAggregator.GetEvent<RuleCommand>().Subscribe(DoProcessRuleCommand);


        }

        private void DoProcessRuleCommand(RuleCommandType commandType)
        {

            switch (commandType)
            {
                case RuleCommandType.AddRule:
                    if (RuleList.IsNullOrEmpty())
                    {
                        RuleList = new ObservableCollection<RuleModel>();
                    }
                    var newRule = new RuleModel()
                    {
                        RuleName = "new rule",
                    };
                    RuleList.Add(newRule);
                    RaisePropertyChanged(nameof(RuleList));

                    //OnPropertyChanged(nameof(RuleList));
                    break;
                case RuleCommandType.DeleteRule:
                    if (RuleList.IsNeitherNullNorEmpty())
                    {
                        //RuleList.Remove(Selected)
                    }
                    break;
                default:
                    break;
            } 

        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            _xmlDatabase = _container?.Resolve<XmlDatabase>();
            if (_xmlDatabase != null)
            {
                RuleList = new ObservableCollection<RuleModel>(_xmlDatabase.Rules.Select(r => RuleModel.FromSerializable(r)));
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}

好的,在Ivoros在評論中給我提示以查看UserControl的DataContext之后,我查看了View.xaml.cs后面的代碼。

原來我忘了在公共View屬性上返回DataContext

之前:

public IMiCasaViewModel ViewModel { get; set; }

后:

public IMiCasaViewModel ViewModel 
{ 
    get
    {
        return (INormalizationViewModel)DataContext;
    }
    set
    {
        DataContext = value;
    }
}

謝謝!!

暫無
暫無

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

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