首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

,ListView怎么修改绑定后的数据

2012-12-21 
求助,ListView如何修改绑定后的数据XAML 定义了一个listviewListView x:NamelvwFeeds SelectionMode

求助,ListView如何修改绑定后的数据
XAML 定义了一个listview

<ListView x:Name="lvwFeeds" SelectionMode="Single" ItemsSource="{Binding Mode=TwoWay}" HorizontalAlignment="Left" Height="412" Margin="129,63,0,0" VerticalAlignment="Top" Width="1109" Grid.Row="1" Foreground="Black">
        <ListView.ItemTemplate>
                <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                                <TextBox Height="50" TextWrapping="Wrap" Text="{Binding Title}" Width="300"/>
                                <TextBox Height="50" TextWrapping="Wrap" Text="{Binding Url}" Width="800"/>
                                <TextBlock Height="50" Width="100" Style="{StaticResource PageHeaderTextStyle}"></TextBlock>
                        </StackPanel>
                </DataTemplate>
        </ListView.ItemTemplate>
</ListView>


C#
在LoadState中调用以下方法
private async void LoadFeedsFromFile()
{
        _categoryHelper.Get_all_categories();
        foreach (Category category in _categoryHelper.Categories)
        {
                feeds.Add(new FeedCategory(category.Url, category.Name));
        }
        lvwFeeds.DataContext = feeds;
}


数据可正常显示,我修改了listview中的信息后,保存,后台取不到更改后的模型或listview的值
private void btnSaveItem_Click(object sender, RoutedEventArgs e)
{
        List<Category> categories = new List<Category>();
        // 取模型中的值取不到
        foreach (FeedCategory feed in feeds)
        {
                Category category = new Category();
                category.Name = feed.Title;
                category.Url = feed.Url;
                category.Visible = true;
                categories.Add(category);
        }


        // 取listview的值也去不到。。。
        foreach (object item in lvwFeeds.Items)
        {
                Category category = new Category();
                category.Name = "";
                category.Url = "";
                category.Visible = true;
                categories.Add(category);
        }

        _categoryHelper.Put_categories(categories);

        ShowMessageDialog("保存成功", "提示");
}


feeds是这样定义的
private ObservableCollection<FeedCategory> feeds = new ObservableCollection<FeedCategory>();

FeedCategory是这样定义的
class FeedCategory : INotifyPropertyChanged
{
        public FeedCategory(string url, string title)
        {
                Url = url;
                Title = title;
        }

        public string _title;
        public string _url;

        public string Title
        {
                get
                {
                        return _title;
                }
                set
                {
                        _title = value;
                        if (PropertyChanged != null)
                        {
                                // 通知属性更改了
                                PropertyChanged(this, new PropertyChangedEventArgs("Title"));
                        }


                }
        }

        public string Url
        {
                get
                {
                        return _url;
                }
                set
                {
                        _url = value;
                        if (PropertyChanged != null)
                        {
                                PropertyChanged(this, new PropertyChangedEventArgs("Url"));
                        }
                }
        }
        public event PropertyChangedEventHandler PropertyChanged;
}

我也试过让FeedCategory继承BindableBase,也不行。

求助,感谢大家了
[最优解释]
One of the BindingMode values. The default is BindingMode.OneWay.

应该是 Text="{Binding Title , Mode=TwoWay}"


帖子发重了

热点排行