问题描述
我面临的问题,当我创建多个数据视图的数据表,然后结合工作不正常了新的实例。只有网站管理员看得到帮我理清这个....
有任何人对这个问题的解决方案?请参考下面的详细信息
previous帖子:
我们正在经历上的DataRowView的PropertyChange事件没有引发新的数据视图instances.This似乎是一个ADO的错误,不上头。之后,应用程序创建一个新的数据视图,WPF订阅更改从数据视图的唯一成员的活动(一个DataRowView的),并取消订阅改变从旧的DataRowView事件。后来不知怎的ADO改变了新的数据视图没有任何通知,与旧更换新的DataRowView。现在改变为名称列影响老DataRowView的,和WPF从来没有听到有关他们因为它是听着新的DataRowView。
在此先感谢。
code:
<窗口x:类=WpfStudy.GridView
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
XMLNS:地方=CLR的命名空间:WpfStudy
高度=400宽度=500标题=&GT与多功能使用ItemSource选项绑定列表视图;
< Window.Resources>
<地方:NameConverter X:关键=NameConverter/>
< /Window.Resources>
<电网>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition />
< RowDefinition />
< /Grid.RowDefinitions>
< StackPanel的>
<按钮单击=Button_Click>点击< /按钮>
< ListView控件名称=ListviewALL>
< ListView.View>
< GridView控件>
< GridViewColumn WIDTH =140标题=绑定标签>
< GridViewColumn.DisplayMemberBinding>
< MultiBinding转换器={的StaticResource NameConverter}>
<绑定路径=名字/>
<绑定路径=名字/>
< / MultiBinding>
< /GridViewColumn.DisplayMemberBinding>
< / GridViewColumn>
&所述; GridViewColumn宽度=140>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=名字的FontFamily =龙力控制台文本={结合名字,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
&所述; GridViewColumn宽度=140>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=姓氏的FontFamily =龙力控制台文本={结合姓氏,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< / GridView的>
< /ListView.View>
< / ListView控件>
< / StackPanel的>
&所述; ListView的名称=列表视图Grid.Row =1>
< ListView.View>
< GridView控件>
< GridViewColumn WIDTH =140标题=绑定标签>
< GridViewColumn.DisplayMemberBinding>
< MultiBinding转换器={的StaticResource NameConverter}>
<绑定路径=名字/>
<绑定路径=名字/>
< / MultiBinding>
< /GridViewColumn.DisplayMemberBinding>
< / GridViewColumn>
&所述; GridViewColumn宽度=140>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=名字的FontFamily =龙力控制台文本={结合名字,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
&所述; GridViewColumn宽度=140>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=姓氏的FontFamily =龙力控制台文本={结合姓氏,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< / GridView的>
< /ListView.View>
< / ListView控件>
&所述; ListView的名称=是ListView1Grid.Row =2>
< ListView.View>
< GridView控件>
< GridViewColumn WIDTH =140标题=绑定标签>
< GridViewColumn.DisplayMemberBinding>
< MultiBinding转换器={的StaticResource NameConverter}>
<绑定路径=名字/>
<绑定路径=名字/>
< / MultiBinding>
< /GridViewColumn.DisplayMemberBinding>
< / GridViewColumn>
&所述; GridViewColumn宽度=140>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=名字的FontFamily =龙力控制台文本={结合名字,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
&所述; GridViewColumn宽度=140>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=姓氏的FontFamily =龙力控制台文本={结合姓氏,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< / GridView的>
< /ListView.View>
< / ListView控件>
< /网格>
< /窗>
转换:
命名空间WpfStudy
{
公共类NameConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型TARGETTYPE,对象参数,
System.Globalization.CultureInfo文化)
{
StringBuilder的SB =新的StringBuilder();
的foreach(在值字符串VAL)
{
如果(sb.Length!= 0)
sb.Append();
sb.Append(VAL);
}
返回sb.ToString();
}
公共对象[] ConvertBack(对象的值,键入[] targetTypes,对象参数,
System.Globalization.CultureInfo文化)
{
抛出新的NotImplementedException();
}
}
}
绑定源
- 我绑定数据表中源3列表视图与目前存在的数据视图。如果我们创造更多的视图实例,然后双向绑定是不是working.xaml没有从源代码更新。
私人无效loadDatatable()
{
数据表DT =新的DataTable();
dt.Columns.Add(名字);
dt.Columns.Add(名字);
dt.Columns.Add(用户ID);
DataRow的博士= dt.NewRow();
博士[0] =希瓦;
博士[1] =贾根;
博士[2] =JS;
dt.Rows.Add(DR);
DataRow的DR1 = dt.NewRow();
DR1 [0] =RAAM;
DR1 [1] =约翰;
DR1 [2] =RJ;
dt.Rows.Add(DR1);
ListviewALL.ItemsSource = dt.DefaultView;
DV1 =新的数据视图(DT);
dv1.RowFilter =名字一样'S%';
DV2 =新的数据视图(DT);
dv2.RowFilter =名字就像'R%';
Listview.ItemsSource = DV1;
Listview1.ItemsSource = DV2;
}
当你把数据视图的BindingListCollectionView或ICollectionView的,而不是来过滤你的东西会发生什么?
编辑:
我尝试过了,并以下工作:
公共BindingListCollectionView视图1 {获得;组; }
公共BindingListCollectionView视图2 {获得;组; }
视图1 =新BindingListCollectionView(新数据视图(DT){RowFilter,它=名字就像'R%'});
视图2 =新BindingListCollectionView(新数据视图(DT){RowFilter,它=名字就像'S%'});
添加,修改和删除的作品。
EDIT2我的例子code:更新fullfill您的要求。通过文本框在任何列表视图作品编辑!
公共类视图模型
{
公开数据表申{获得;组; }
公共BindingListCollectionView视图1 {获得;组; }
公共BindingListCollectionView视图2 {获得;组; }
公共视图模型()
{
DT =新的DataTable();
Dt.Columns.Add(姓名);
Dt.Columns.Add(VorName);
DataRow的博士= Dt.NewRow();
博士[0] =希瓦;
博士[1] =贾根;
Dt.Rows.Add(DR);
DataRow的DR1 = Dt.NewRow();
DR1 [0] =RAAM;
DR1 [1] =约翰;
Dt.Rows.Add(DR1);
视图1 =新BindingListCollectionView(新数据视图(DT){RowFilter,它=名称像'R%'});
视图2 =新BindingListCollectionView(新数据视图(DT){RowFilter,它=名称类似'S%'});
}
}
window.xaml.cs
公共部分类主窗口:窗口
{
私人视图模型_data;
公共主窗口()
{
的InitializeComponent();
this._data =新视图模型();
this.DataContext = _data;
}
}
window.xaml
< Window.Resources>
<地方:NameConverter X:关键=NameConverter/>
< /Window.Resources>
<电网>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =138 */>
< ColumnDefinition WIDTH =209 */>
< ColumnDefinition WIDTH =156 */>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition身高=76 */>
< RowDefinition高度=235 */>
< /Grid.RowDefinitions>
&所述; ListView中的ItemsSource ={结合申}Grid.Column =0Grid.Row =1>
< ListView.View>
< GridView控件>
< GridViewColumn WIDTH =140标题=绑定标签>
< GridViewColumn.DisplayMemberBinding>
< MultiBinding转换器={的StaticResource NameConverter}>
<绑定路径=名称/>
<绑定路径=VorName/>
< / MultiBinding>
< /GridViewColumn.DisplayMemberBinding>
< / GridViewColumn>
< GridViewColumn标题=名称>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=名字的FontFamily =龙力控制台文本={结合名称,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn标题=VorNameDisplayMemberBinding ={结合VorName}>
< / GridViewColumn>
< / GridView的>
< /ListView.View>
< / ListView控件>
&所述; ListView中的ItemsSource ={结合视图1}Grid.Column =1Grid.Row =1>
< ListView.View>
< GridView控件>
< GridViewColumn WIDTH =140标题=绑定标签>
< GridViewColumn.DisplayMemberBinding>
< MultiBinding转换器={的StaticResource NameConverter}>
<绑定路径=名称/>
<绑定路径=VorName/>
< / MultiBinding>
< /GridViewColumn.DisplayMemberBinding>
< / GridViewColumn>
< GridViewColumn标题=名称>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=名字的FontFamily =龙力控制台文本={结合名称,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn标题=VorNameDisplayMemberBinding ={结合VorName}>
< / GridViewColumn>
< / GridView的>
< /ListView.View>
< / ListView控件>
&所述; ListView中的ItemsSource ={结合视图2}Grid.Column =2Grid.Row =1>
< ListView.View>
< GridView控件>
< GridViewColumn WIDTH =140标题=绑定标签>
< GridViewColumn.DisplayMemberBinding>
< MultiBinding转换器={的StaticResource NameConverter}>
<绑定路径=名称/>
<绑定路径=VorName/>
< / MultiBinding>
< /GridViewColumn.DisplayMemberBinding>
< / GridViewColumn>
< GridViewColumn标题=名称>
< GridViewColumn.CellTemplate>
<的DataTemplate>
<文本框名称=名字的FontFamily =龙力控制台文本={结合名称,模式=双向,UpdateSourceTrigger =的PropertyChanged}WIDTH =200/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn标题=VorNameDisplayMemberBinding ={结合VorName}>
< / GridViewColumn>
< / GridView的>
< /ListView.View>
< / ListView控件>
< /网格>
I am facing issue when i am creating more than one dataview for datatable then binding is not working properly for new instances. Pleae help me to sort out this....
Have anyone have solution for this issue ? Please refer below for more details
Previous Post:
" we are experiencing the PropertyChange event on DataRowView not being raised for new Dataview instances.This appears to be an ADO bug, not WPF. After the app creates a new DataView, WPF subscribes to change events from the DataView's only member (a DataRowView), and unsubscribes to change events from the old DataRowView. Then somehow ADO changes the new DataView without any notification, replacing the new DataRowView with the old one. Now changes to the Name column affect the old DataRowView, and WPF never hears about them because it's listening to the new DataRowView."
Thanks in advance..
Code :
<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>
Converter :
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();
}
}
}
Binding Source
- 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;
}
what happens when you take a BindingListCollectionView or ICollectionView instead of the Dataview to filter your stuff?
EDIT:
i tried it out and the following works:
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%'"});
Add, Modify and Delete works.
EDIT2 my sample code: update to fullfill your requirements. editing through the textbox in any listview works!
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>
这篇关于WPF绑定与数据视图不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!