1

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

有没有人有这个问题的解决方案?请参阅下文了解更多详情

上一篇:

“我们在 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;

        }
4

2 回答 2

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%'"});

    }
}

窗口.xaml.cs

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

        this.DataContext = _data;
    }
}

窗口.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>   
于 2012-04-18T06:28:57.293 回答
0

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

  dt.AcceptChanges();

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

  var tempFilter = dt.DefaultView.RowFilter;
  dt.DefaultView.RowFilter = string.empty;
  dt.DefaultView.RowFilter = tempFilter;
于 2012-04-18T08:13:53.017 回答