当我为数据表创建多个数据视图时,我遇到了问题,然后绑定对于新实例无法正常工作。请帮我解决这个问题......
有没有人有这个问题的解决方案?请参阅下文了解更多详情
上一篇:
“我们在 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;
}