2015-11-13 95 views
0

請注意,我是WPF的新手,我使用的WebService返回gridlist[],它會自動填充我的Gridview。這裏是WebService如何從列表視圖/ Gridview中提取某些數據單元wpf

C#:

WebService.Contacts Contact = new WebService.Contacts(); 
grdGetGroup.ItemsSource = Contact.GetGroups(Username, Password); 

這裏是我的XAML

<ListView x:Name="grdGetGroup" 
      Margin="560,34,128,48" 
      FontSize="13" 
      BorderBrush="#FFF01F1F" 
      Foreground="#FFF01F1F" 
      SelectedIndex="1" 
      FontFamily="/WPF Working Experimenet;component/Font/#B Nazanin"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn x:Name="GridID" 
          Header="ID" 
          Width="50" 
          DisplayMemberBinding="{Binding GroupID}" 
          FrameworkElement.FlowDirection="RightToLeft" /> 
      <GridViewColumn Header="Group Name" 
          Width="85 
                  " 
          DisplayMemberBinding="{Binding GroupName}" 
          FrameworkElement.FlowDirection="RightToLeft" /> 
      <GridViewColumn Header="Numbers" 
          Width="60" 
          DisplayMemberBinding="{Binding ContactCount}" 
          FrameworkElement.FlowDirection="RightToLeft" /> 
      <GridViewColumn Header="Access" 
          Width="60" 
          DisplayMemberBinding="{Binding ShowToChild}" 
          FrameworkElement.FlowDirection="RightToLeft" /> 
      <GridViewColumn Header="Description" 
          Width="150" 
          DisplayMemberBinding="{Binding GroupDescription}" 
          FrameworkElement.FlowDirection="RightToLeft" /> 
      <GridViewColumn Header="" 
          Width="60" 
          FrameworkElement.FlowDirection="RightToLeft"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <Button x:Name="btnChangeGroup" 
            Margin="5" 
            Content="Change" 
            Cursor="Hand" 
            Click="btnChangeGroup_Click"> 
           <Button.Template> 
            <ControlTemplate TargetType="Button"> 
             <TextBlock TextDecorations="Underline"> 
                      <ContentPresenter /></TextBlock> 
            </ControlTemplate> 
           </Button.Template> 
           <Button.Style> 
            <Style TargetType="Button"> 
             <Setter Property="Foreground" 
               Value="Black" /> 
             <Style.Triggers> 
              <Trigger Property="IsMouseOver" 
                Value="true"> 
               <Setter Property="Foreground" 
                 Value="Red" /> 
              </Trigger> 
             </Style.Triggers> 
            </Style> 
           </Button.Style> 
          </Button> 
         </StackPanel> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn Header="" 
          Width="60" 
          FrameworkElement.FlowDirection="RightToLeft"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <Button x:Name="btnRemoveGroup" 
            Margin="5" 
            Content="Remove" 
            Cursor="Hand" 
            Click="btnRemoveGroup_Click"> 
           <Button.Template> 
            <ControlTemplate TargetType="Button"> 
             <TextBlock TextDecorations="Underline"> 
                      <ContentPresenter /></TextBlock> 
            </ControlTemplate> 
           </Button.Template> 
           <Button.Style> 
            <Style TargetType="Button"> 
             <Setter Property="Foreground" 
               Value="Black" /> 
             <Style.Triggers> 
              <Trigger Property="IsMouseOver" 
                Value="true"> 
               <Setter Property="Foreground" 
                 Value="#FFF01F1F" /> 
              </Trigger> 
             </Style.Triggers> 
            </Style> 
           </Button.Style> 
          </Button> 
         </StackPanel> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView.View> 
</ListView> 

如果你已經注意到了,我有2個button,每行Gridview這是建立自動以及,現在這裏是我的問題如何提取在GridviewColumn x:Name"GridID"?並把它傳遞到同一行的Button Click Event(其中有一個method,是以Data有一個輸入)

+0

你想使用GridID按鈕嗎?你想要處理GridID來處理按鈕的回調嗎? –

+0

嗨@EmmanuelDURIN,我有另一個Web服務方法,它將GridID的數據作爲輸入並處理其他內容。我必須將單元格的數據存儲到另一個變量中並在該方法中使用它。它必須動態發生,因爲每一行都有它自己的數據和它的按鈕,並且進程只發生在同一行。 – Valkyry

回答

1

Valkyry,

如果我已瞭解/猜的沒錯,在每一行,你已經有了一個gridList項目。

如果我是undestand/guess right,則每個gridList都有GroupID屬性。
如果我錯了,糾正我。

在WPF中,在DataGrid中,每一行都有一個DataContext屬性,持有該行的數據:

void btnRemoveGroup_Click(Object sender, EventArgs args) 
{ 
    var fxElt = sender as FrameworkElement; 
    var lineData = fxElt.DataContext as gridlist; 
    int groupID = lineData .GroupID; 
} 

告訴我它的工作與否。問候

+0

它工作得很好!你是我的救星! :D謝謝!你值得+100這:) – Valkyry