2017-11-03 6 views
0

나는 단추 태그에 바인딩 된 단추에서 ID를 가져 오려고하지만 상황에 텍스트 및 문맥을 설정할 수 있지만 화면에 Id가 표시 되더라도 null 값 오류가 발생합니다. 벌금.UWP 툴킷 PullToRefreshList에서 항목의 색인을 가져 오는 방법이 있습니까?

이제는 목록의 항목을 색인으로 표시하려고합니다. 그래서 데이터 템플릿에서 해당 항목을 생성 할 때 쉽게 호출하고 내 C# 코드의 ID와 비교할 수 있습니다. 목록

XAML : 이드의 결합을 시도 메신저 나중에 검색 할 경우 스택 패널에서

<my:PullToRefreshListView 
    x:Name="RefreshListView" 
    MinWidth="200" 
    Margin="24" 
    HorizontalAlignment="Center" 
    VerticalAlignment="Bottom" 
    Background="White" 
    OverscrollLimit="0.4" 
    PullThreshold="100" 
    IsPullToRefreshWithMouseEnabled="True"> 
     <my:PullToRefreshListView.ItemTemplate > 
      <DataTemplate > 
       <StackPanel Name="ListPanel"> 
         <TextBlock AutomationProperties.Name="IdTextBlock" 
      Style="{StaticResource CaptionTextBlockStyle}" 
      Text="{Binding Id}" 
      TextWrapping="WrapWholeWords" /> 
         <TextBlock AutomationProperties.Name="{Binding Name}" 
      Style="{StaticResource CaptionTextBlockStyle}" 
      Text="{Binding Name}" 
      TextWrapping="WrapWholeWords" /> 
        <TextBlock AutomationProperties.Name="{Binding Sets}" 
      Style="{StaticResource CaptionTextBlockStyle}" 
      Text="{Binding Sets}" 
      TextWrapping="WrapWholeWords" /> 
        <TextBlock AutomationProperties.Name="{Binding SetTime}" 
      Style="{StaticResource CaptionTextBlockStyle}" 
      Text="{Binding SetTime}" 
      TextWrapping="WrapWholeWords" /> 
         <StackPanel Orientation="Horizontal"> 
          <Button Tag="{Binding Id}" Content="Delete" Click="DelButton_Click" Style="{StaticResource DrillButtons}" ></Button> 
          <Button Tag="{Binding Id}" Content="Update" Click="UpdateBtn_Click" Style="{StaticResource DrillButtons}" ></Button> 
         </StackPanel> 
        </StackPanel> 
      </DataTemplate> 
     </my:PullToRefreshListView.ItemTemplate> 
     <my:PullToRefreshListView.PullToRefreshContent> 
      <TextBlock FontSize="16" 
      Opacity="0.5" 
      Text="Pull down to refresh data" /> 
     </my:PullToRefreshListView.PullToRefreshContent> 
    </my:PullToRefreshListView 

이 두 버튼 위입니다. 버튼에서 아이디에 걸릴

<StackPanel Orientation="Horizontal"> 
          <Button Tag="{Binding Id}" Content="Delete" Click="DelButton_Click" Style="{StaticResource DrillButtons}" ></Button> 
          <Button Tag="{Binding Id}" Content="Update" Click="UpdateBtn_Click" Style="{StaticResource DrillButtons}" ></Button> 
         </StackPanel> 

을 Heres 방법 :

//Update button 
    private async void NewSubmitBtn_Click(object sender, RoutedEventArgs e) 
    { 
     String Name = NewNameBox.Text; 
     String id = (String)((Button)sender).Content; 
     int Sets; 
     int Time; 
     bool successfullyParsedTime = int.TryParse(NewSetsBox.Text, out Time); 
     bool successfullyParsedSets = int.TryParse(NewTimeBox.Text, out Sets); 

     if (successfullyParsedSets) 
     { 
      Sets = Int32.Parse(NewSetsBox.Text); 

     } 
     if (successfullyParsedTime) 
     { 
      Time = Int32.Parse(NewTimeBox.Text); 
     } 

     await ctv.combatDrillsTable.UpdateDrill(id, Name, Sets, Time, catagory); 
     ppup.IsOpen = false; 
     var addSuccess = new MessageDialog("Drill Updated"); 
     await addSuccess.ShowAsync(); 

    } 

Heres는 데이터 항목 코드 : I 추가

namespace UWPCombatApp 
{ 
class DrillItem 
{ 
    public string Id { get; set; } 

    [JsonProperty(PropertyName = "name")] 
    public string Name { get; set; } 

    [JsonProperty(PropertyName = "sets")] 
    public int Sets { get; set; } 

    [JsonProperty(PropertyName = "settime")] 
    public int SetTime { get; set; } 

    [JsonProperty(PropertyName = "style")] 
    public string Style { get; set; } 

} 
} 

h 제 팝업 :

// Delete button 
    private void DelButton_Click(object sender, RoutedEventArgs e) 
    { 
     delpup.Height = Window.Current.Bounds.Height; 
     delpup.IsOpen = true; 
     id = (((Button)sender).Tag).ToString(); 
    } 

id는 this 태그를 사용하여 바인딩됩니다.

private async void YesBtn_Click(object sender, RoutedEventArgs e) 
    { 
     await ctv.combatDrillsTable.DeleteDrillAsync(id); 
     var addSuccess = new MessageDialog("Drill Deleted"); 
     await addSuccess.ShowAsync(); 

    } 

답변

0

이벤트 핸들러 내부 :

private async void NewSubmitBtn_Click(object sender, RoutedEventArgs e) 

당신이 ID 변수의 값을 설정하는 그들이 선택하면 그래 다음 함수는 데이터베이스에서 항목을 삭제하기 위해 호출됩니다 태그 대신 내용이 인 단추입니다.

변경이에

String id = (String)((Button)sender).Content; 

:

:

String id = (String)((Button)sender).Tag; 

이유로 인해 당신의 핸들러 를 호출하는 업데이트 버튼의 클릭 이벤트에 적합한 이벤트 핸들러를 호출해야합니다

<Button Tag="{Binding Id}" Content="Update" Click="UpdateBtn_Click" Style="{StaticResource DrillButtons}" ></Button> 

대신

<Button Tag="{Binding Id}" Content="Update" Click="NewSubmitBtn_Click" Style="{StaticResource DrillButtons}" ></Button> 

행운을 빈다.

+0

나는 actully 태그를 설정하고 updatebtn은 NewSubmitBtn을 호출하는 팝업을 호출했다. 나는 당신의 구현을 시도했지만 여전히 null과 같은 결과를 얻었다. – UWP122

+0

@ UWP122 더 많은 정보와 코드를 제공하십시오. 실제 문제가 무엇인지 명확하지 않습니다. ** UpdateBtn_Click **이 ** NewSubmit_Btn_Click ** 처리기에 연결되어있는 버튼이 포함 된 MessageDialog를 시작하면 보낸 사람은 MessageDialog 내의 버튼입니다. 해당 버튼에도 태그가 설정되어 있습니까? –

+0

당신이 물어 본 것과 같은 코드를 업데이트했습니다. 기본적으로 내가하고 싶은 일은 사용자가 클릭 한 항목에 따라 항목 목록에서 제공되는 목록에 표시된 데이터 템플릿에서 ID를 가져 오는 것입니다. Azure 포털 쉬운 테이블에서 삭제하십시오. – UWP122