2017-10-06 79 views
2

我的問題是,如何使用超鏈接單擊事件從其他列中獲取綁定數據?如何從GridView列中的超鏈接單擊事件獲取綁定數據

我有一個GridView,它顯示我的自定義類的數據.GridView包含4列,其中一個有超鏈接。

XML:

<ListView Name="CCYVIEW"> 
       <ListView.View> 
        <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Authors"> 
         <GridViewColumn Header="CurrencyName" Width="120" DisplayMemberBinding="{Binding Path=CurrencyName}" /> 
         <GridViewColumn Header="CurrencyTitle" Width="122" DisplayMemberBinding="{Binding Path=CurrencyTitle}" /> 
         <GridViewColumn Header="BaseCurrency" Width="122" DisplayMemberBinding="{Binding Path=BaseCurrency}" /> 
         <GridViewColumn Width="170"> 
          <GridViewColumn.CellTemplate> 
           <DataTemplate> 
            <TextBlock> 
            <Hyperlink Foreground="#FFF7CA00" Click="Hyperlink_Click"> Add to market watch</Hyperlink> 
            </TextBlock> 
           </DataTemplate> 
          </GridViewColumn.CellTemplate> 
         </GridViewColumn> 
        </GridView> 
       </ListView.View> 
      </ListView> 

現在我想的是,當我從第三或第四列單擊超鏈接。它應該給我所有來自第三或第四列(CurrencyName,Currencytitle等)的數據。

+0

在列表視圖中將'SelectedItem'綁定到'ViewModel'中的一個屬性。 – XAMlMAX

+0

我已經在後面的代碼中完成了。所有三列都顯示數據,因爲我將它們綁定在一起。但是,當我點擊特定列中的超鏈接時,我想要檢索這些數據。 –

+0

'SelectedItem'將爲每列保存** ALL **數據。 – XAMlMAX

回答

0

首先,讓我們在觀看基準視圖模型:

xmlns:vm="clr-namespace:VM;assembly=VM"//you will need to adapt this to the structure of your project. 

現在將其設置爲DataContext之一:

<Window.DataContext> 
    <vm:MainViewModel/> 
</Window.DataContext> 

或者:

<UserControl.DataContext> 
    <vm:MainViewModel/> 
</UserControl.DataContext> 

在XAML中你應該有列表視圖定義如下:

<ListView Name="CCYVIEW" SelectedItem="{Binding NameOfTheVMProperty}"> 

然後在您的視圖模型,你應該有這樣的特性:

private object _selectedItem; 

public object SelectedItem //because you haven't specified the type I am using an object here 
{ 
    get { return _selectedItem; } 
    set { _selectedItem = value; OnPropertyChanged("SelectedItem"); } 
} 

在你的項目在您的視圖模型選定了這一點,所以讓我們去到事件處理程序:

private void Hyperlink_OnClick(object sender, RoutedEventArgs e) 
{ 
    var vm = this.DataContext as MainViewModel; 
    vm.SelectedItem;//this is where you now have access to the selected item 
} 

現在您可能沒有選擇該項目,因此您可以將點擊元素的父項作爲ListViewItem,然後獲取DataContext

相關問題