与Dataview的WPF绑定无法正常工作

时间:2012-04-18 01:13:24

标签: wpf xaml data-binding ado.net

我在为数据表创建多个数据视图时遇到问题,然后绑定对新实例无法正常工作。请帮助我解决这个问题....

有没有人可以解决这个问题?请参阅下面的更多详细信息

上一篇文章:

“我们遇到的DataRowView上的PropertyChange事件没有为新的Dataview实例引发。这似乎是一个ADO错误,而不是WPF。在应用程序创建一个新的DataView之后,WPF订阅了从DataView唯一的成员更改事件(一个DataRowView),并取消订阅以更改旧DataRowView中的事件。然后以某种方式ADO更改新DataView而不通知任何内容,用旧的DataRowView替换新的DataRowView。现在对Name列的更改会影响旧的DataRowView,而WPF从不会听到因为它正在收听新的DataRowView。“

提前致谢..

代码:

<Window x:Class="WpfStudy.GridView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfStudy"
         Height="400" Width="500" Title="Binding Listview with Mulit ItemSource Options">
    <Window.Resources>
        <local:NameConverter  x:Key="NameConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <StackPanel>
            <Button Click="Button_Click">Click</Button>
            <ListView Name="ListviewALL" >
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="140" Header="Binding Label" >
                            <GridViewColumn.DisplayMemberBinding>
                                <MultiBinding Converter="{StaticResource NameConverter}">
                                    <Binding  Path="LastName" />
                                    <Binding  Path="FirstName" />
                                </MultiBinding>
                            </GridViewColumn.DisplayMemberBinding>
                        </GridViewColumn>

                        <GridViewColumn Width="140">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Name="FirstName" FontFamily="Lucida Console" Text="{Binding FirstName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Width="140">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Name="LastName" FontFamily="Lucida Console" Text="{Binding LastName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>


                    </GridView>
                </ListView.View>
            </ListView>

        </StackPanel>
        <ListView Name="Listview" Grid.Row="1">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Binding Label" >
                        <GridViewColumn.DisplayMemberBinding>
                            <MultiBinding Converter="{StaticResource NameConverter}">
                                <Binding  Path="LastName" />
                                <Binding  Path="FirstName" />
                            </MultiBinding>
                        </GridViewColumn.DisplayMemberBinding>
                    </GridViewColumn>

                    <GridViewColumn Width="140">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Name="FirstName" FontFamily="Lucida Console" Text="{Binding FirstName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="140">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Name="LastName" FontFamily="Lucida Console" Text="{Binding LastName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>


                </GridView>
            </ListView.View>
        </ListView>

        <ListView Name="Listview1" Grid.Row="2">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Binding Label" >
                        <GridViewColumn.DisplayMemberBinding>
                            <MultiBinding Converter="{StaticResource NameConverter}">
                                <Binding  Path="LastName" />
                                <Binding  Path="FirstName" />
                            </MultiBinding>
                        </GridViewColumn.DisplayMemberBinding>
                    </GridViewColumn>

                    <GridViewColumn Width="140">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Name="FirstName" FontFamily="Lucida Console" Text="{Binding FirstName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="140">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Name="LastName" FontFamily="Lucida Console" Text="{Binding LastName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>


                </GridView>
            </ListView.View>
        </ListView>



    </Grid>
</Window>

转换器:

namespace WpfStudy
{
    public class NameConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {

            StringBuilder sb=new StringBuilder();

            foreach(string val in values)
            {

                if (sb.Length != 0)
                    sb.Append(" ");

                    sb.Append(val);

            }

            return sb.ToString();
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}

绑定源

- i am binding datatable source with 3 list view with differnet dataview. if we create more view instance then two way binding is not working.xaml not getting updated from source.

  private void loadDatatable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName");
            dt.Columns.Add("LastName");
            dt.Columns.Add("Userid");


            DataRow dr = dt.NewRow();
            dr[0] = "Siva";
            dr[1] = "Jagan";
            dr[2] = "JS";
            dt.Rows.Add(dr);

            DataRow dr1 = dt.NewRow();
            dr1[0] = "Raam";
            dr1[1] = "John";
            dr1[2] = "RJ";
            dt.Rows.Add(dr1);


            ListviewALL.ItemsSource = dt.DefaultView;

            dv1 = new DataView(dt);
            dv1.RowFilter = "FirstName like'S%'";


            dv2 = new DataView(dt);
            dv2.RowFilter = "FirstName like 'R%'";


            Listview.ItemsSource = dv1;
            Listview1.ItemsSource = dv2;

        }

2 个答案:

答案 0 :(得分:1)

当你使用BindingListCollectionView或ICollectionView而不是Dataview过滤你的东西时会发生什么?

编辑:

我尝试了以下工作:

    public BindingListCollectionView View1 { get; set; }
    public BindingListCollectionView View2 { get; set; }

    View1 = new BindingListCollectionView(new DataView(Dt) { RowFilter = "FirstName like 'R%'" });
    View2 = new BindingListCollectionView(new DataView(Dt) { RowFilter = "FirstName like 'S%'"});

添加,修改和删除作品。

EDIT2我的示例代码:更新以满足您的要求。通过任何列表视图中的文本框进行编辑工作!

public class Viewmodel
{
    public DataTable Dt { get; set; }
    public BindingListCollectionView View1 { get; set; }
    public BindingListCollectionView View2 { get; set; }

    public Viewmodel()
    {
        Dt = new DataTable();
        Dt.Columns.Add("Name");
        Dt.Columns.Add("VorName");


        DataRow dr = Dt.NewRow();
        dr[0] = "Siva";
        dr[1] = "Jagan";
        Dt.Rows.Add(dr);

        DataRow dr1 = Dt.NewRow();
        dr1[0] = "Raam";
        dr1[1] = "John";
        Dt.Rows.Add(dr1);


        View1 = new BindingListCollectionView(new DataView(Dt) { RowFilter = "Name like 'R%'" });
        View2 = new BindingListCollectionView(new DataView(Dt){RowFilter = "Name like 'S%'"});

    }
}

window.xaml.cs

public partial class MainWindow : Window
{
    private Viewmodel _data;
    public MainWindow()
    {
        InitializeComponent();
        this._data = new Viewmodel();

        this.DataContext = _data;
    }
}

window.xaml

 <Window.Resources>
    <local:NameConverter  x:Key="NameConverter"/>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="138*" />
        <ColumnDefinition Width="209*" />
        <ColumnDefinition Width="156*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="76*" />
        <RowDefinition Height="235*" />
    </Grid.RowDefinitions>     
    <ListView ItemsSource="{Binding Dt}" Grid.Column="0" Grid.Row="1">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="140" Header="Binding Label" >
                    <GridViewColumn.DisplayMemberBinding>
                        <MultiBinding Converter="{StaticResource NameConverter}">
                            <Binding  Path="Name" />
                            <Binding  Path="VorName" />
                        </MultiBinding>
                    </GridViewColumn.DisplayMemberBinding>
                </GridViewColumn>
                <GridViewColumn Header="Name" > 
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="FirstName" FontFamily="Lucida Console" Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>    
                </GridViewColumn>
                <GridViewColumn Header="VorName" DisplayMemberBinding="{Binding VorName}">
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <ListView ItemsSource="{Binding View1}" Grid.Column="1" Grid.Row="1">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="140" Header="Binding Label" >
                    <GridViewColumn.DisplayMemberBinding>
                        <MultiBinding Converter="{StaticResource NameConverter}">
                            <Binding  Path="Name" />
                            <Binding  Path="VorName" />
                        </MultiBinding>
                    </GridViewColumn.DisplayMemberBinding>
                </GridViewColumn>
                <GridViewColumn Header="Name"  > 
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="FirstName" FontFamily="Lucida Console" Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate> 
                </GridViewColumn>
                <GridViewColumn Header="VorName" DisplayMemberBinding="{Binding VorName}">
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <ListView ItemsSource="{Binding View2}" Grid.Column="2" Grid.Row="1">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="140" Header="Binding Label" >
                    <GridViewColumn.DisplayMemberBinding>
                        <MultiBinding Converter="{StaticResource NameConverter}">
                            <Binding  Path="Name" />
                            <Binding  Path="VorName" />
                        </MultiBinding>
                    </GridViewColumn.DisplayMemberBinding>
                </GridViewColumn>

                <GridViewColumn Header="Name"  > 
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="FirstName" FontFamily="Lucida Console" Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate> 

                </GridViewColumn>
                <GridViewColumn Header="VorName" DisplayMemberBinding="{Binding VorName}">

                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>   

答案 1 :(得分:0)

每次添加后试试这个......

  dt.AcceptChanges();

如果这不起作用,则重置行过滤器

  var tempFilter = dt.DefaultView.RowFilter;
  dt.DefaultView.RowFilter = string.empty;
  dt.DefaultView.RowFilter = tempFilter;