我有一個listview,我已經綁定(twoWay)到一個數據表。綁定工作正常,數據在列表視圖中正常顯示。現在我想實現的有點複雜,我甚至不確定這是可以實現的還是現在。DataBinding到列表視圖
我的數據表有15列,我在lisview中顯示5列。如果用戶在列表視圖上選擇一行,可能會在棧面板中的文本塊中顯示該選定行的其他10個值(來自數據表)。這是可以實現的還是我要求太高?我試圖通過從問題here獲得想法來實現這一目標,但無法實現這一點。 如果這是可以實現的,你們可以給我一些關於如何繼續這個的想法嗎?
我認爲這可以通過處理listview1_selectionChanged事件並手動填充文本框來實現,但由於我處於學習階段,因此我想探討一下這是否可以通過數據綁定來完成。通過這種方式,我將瞭解各種做事方式,並可以在此過程中構建我的概念。
我在下面附上我的代碼。這只是一個測試項目,其中一個列表視圖有一列。
XAML:
<Window.Resources>
<Prefs:Tables x:Key="TClass"></Prefs:Tables>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<ListView Name="listView1" Background="Transparent" Height="534" BorderThickness="0 0 0 1" VerticalAlignment="Top">
<ListView.ItemsSource>
<Binding Source="{StaticResource TClass}" Path="Instance.dtAccounts" Mode="TwoWay"></Binding>
</ListView.ItemsSource>
<ListView.View>
<GridView x:Name="GridView1" ColumnHeaderContainerStyle="{StaticResource GridViewHeader}" AllowsColumnReorder="True">
<GridViewColumn Header="Company Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="txbName" Padding="0 0 5 0" >
<TextBlock.Text>
<Binding Path="NAME">
</Binding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Name="stkPanel1" Margin="100 0 0 0">
<TextBlock></TextBlock>
</StackPanel>
</StackPanel>
</Grid>
Window1.xaml.cs
public partial class Window1 : Window
{
DataTable dt = new DataTable();
public Window1()
{
InitializeComponent();
Tables.Instance.dtAccounts = Worker.LoadAccounts();
}
}
類Tables.cs
public class Tables : INotifyPropertyChanged
{
private static Tables instance;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private DataTable _dtAccounts;
public Tables()
{
}
// Singleton instance read-only property
public static Tables Instance
{
get
{
if (instance == null)
{
instance = new Tables();
}
return instance;
}
}
public DataTable dtAccounts
{
get
{
return _dtAccounts;
}
set
{
_dtAccounts = value;
OnPropertyChanged("dtAccounts");
}
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
=====================
最後的工作代碼
我能做到這一點的幫助下Phil提供的答案。在下面發佈我的更新代碼,因爲它可能對其他人有用。
XAML:
<Grid>
<StackPanel Orientation="Horizontal">
<ListView Name="listView1" Background="Transparent" Height="534" BorderThickness="0 0 0 1" VerticalAlignment="Top" SelectionChanged="listView1_SelectionChanged">
<ListView.ItemsSource>
<Binding Source="{StaticResource TClass}" Path="Instance.dtAccounts" Mode="TwoWay"></Binding>
</ListView.ItemsSource>
<ListView.View>
<GridView x:Name="GridView1" ColumnHeaderContainerStyle="{StaticResource GridViewHeader}" AllowsColumnReorder="True">
<GridViewColumn Header="Company Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="txbName" Padding="0 0 5 0" >
<TextBlock.Text>
<Binding Path="NAME">
</Binding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Name="stkPanel1" Margin="100 0 0 0">
<TextBlock>
<TextBlock.Text>
<Binding Source="{StaticResource TClass}" Path="Instance.SelectedName" Mode="TwoWay">
</Binding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</StackPanel>
</Grid>
Window1.xaml.cs
public partial class Window1 : Window
{
DataTable dt = new DataTable();
public Window1()
{
InitializeComponent();
Tables.Instance.dtAccounts = Worker.LoadAccounts();
}
private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView lstView = sender as ListView;
int item = lstView.SelectedIndex;
Tables.Instance.SetSelectedRow(item);
}
}
Tables.cs
public class Tables : INotifyPropertyChanged
{
private static Tables instance;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private DataTable _dtAccounts;
private string _selectedName;
public Tables()
{
}
// Singleton instance read-only property
public static Tables Instance
{
get
{
if (instance == null)
{
instance = new Tables();
}
return instance;
}
}
public DataTable dtAccounts
{
get
{
return _dtAccounts;
}
set
{
_dtAccounts = value;
OnPropertyChanged("dtAccounts");
}
}
public string SelectedName
{
get
{
return _selectedName;
}
set
{
_selectedName = value;
OnPropertyChanged("SelectedName");
}
}
public void SetSelectedRow(int index)
{
int indexNo = index;
SelectedName = dtAccounts.Rows[index][0].ToString();
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
感謝菲爾,它工作得很好。我將在我原來的帖子中粘貼更新的代碼。這可能對別人有幫助。 – 2010-06-24 15:13:20