2017-12-25 22 views
0

검색 및 검색하고 검색했지만 아주 간단한 문제로 생각할 수도있는 답변을 찾지 못하는 것 같습니다. .목록보기에서 텍스트 상자의 항목 색인을 얻는 방법 또는 설정하십시오.

사용자가 단추 및 OpenFileDialog를 사용하여 항목을 추가 할 수있는 listview가 있습니다. 이 작동합니다. 리스트 뷰에서

한 열은 텍스트 상자로 구성되어 다음과 같이

문제가있다. 사용자가 텍스트 상자에 (숫자) 값을 추가 할 수 있고 그 값이 해당 행에있는 항목과 연관 될 수 있기를 바랍니다. 열 중 하나에 텍스트 상자를 만들었고 매우 광범위한 검색을 한 후에 값 (즉, 텍스트 상자 값)을 얻기 위해 LostFocus 옵션을 추가했습니다. 이제 문제는 listview에서 선택된 항목의 색인이 항상 설정되지는 않았을 것입니다 (아마도 lostfocus가 호출되기 전에 항목이 선택 되었기 때문일 수 있습니다). 이 결과는 selectedindex가 -1이므로 어떤 항목이 텍스트 상자의 텍스트와 연결되어야하는지 알지 못합니다.

나는 올바른 항목을 선택하는 데 도움이 될 마우스 클릭 이벤트를 사용해 보았지만 효과가 없었습니다. 내가 목록보기 내가 건너 온 WPF 많은 솔루션을 사용하고

는 관련이없는 것 같다. 또한, wpf (그리고 아마추어 프로그래머)에게 매우 익숙하기 때문에 복잡한 솔루션을 이해하는 데 어려움을 겪고 있습니다 ... 그러나이 문제는 복잡하지 않을 수도 있습니다.

제발 도와주세요.

내 코드입니다 :

<ListView.View> 
    <GridView> 
     <GridViewColumn Width="30" Header="Num" DisplayMemberBinding="{Binding Num}" /> 
     <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}" /> 
     <GridViewColumn Width="50" Header="FromPage"> 
      <GridViewColumn.CellTemplate> 
       <DataTemplate> 
        <TextBox x:Name="txt_TBFromPage" LostFocus="txt_FromPage_LostFocus" Text="{Binding SelectedItem, Mode=TwoWay, ElementName=txtValue}"/> 
       </DataTemplate> 
      </GridViewColumn.CellTemplate> 
     </GridViewColumn> 
    </GridView> 
</ListView.View> 

이는 C# 부분입니다 : 다음을 추가 할 수

private void txt_FromPage_LostFocus(object sender, RoutedEventArgs e){ 
    int Index = MyListView.SelectedIndex; 

    Index = MyListView.Items.IndexOf(MyListView.SelectedItem); 

    try { 
     //Get cell value by using sender Object 
     string TextValue = ((System.Windows.Controls.TextBox)sender).Text; 

     MyItem item = (MyItem)MyListView.Items[Index]; 
     item.FromPage = TextValue; 

    } 

    catch (Exception) { 

    } 
} 

답변

0

:

.cs

private void txt_TBFromPage_GotFocus(object sender, RoutedEventArgs e) 
    { 
     this.MyListView.SelectedItem = (sender as FrameworkElement).DataContext; 
    } 

. xaml

<TextBox x:Name="txt_TBFromPage" GotFocus="txt_TBFromPage_GotFocus" LostFocus="txt_TBFromPage_LostFocus" Text="{Binding SelectedItem, Mode=TwoWay, ElementName=txtValue}" /> 

필요에 맞습니까?

+0

예! 밀라노에게 감사드립니다! – dorgoldi