2012-04-03 3 views
0

코드에는 DataGrid의 버튼이 있습니다. 이 각각은 텍스트 "보내기"를 포함합니다.이벤트 핸들러를 사용하여 버튼의 내용을 변경하십시오.

<DataGrid.Columns> 
    <DataGridTextColumn Width="*" 
         Header="Uid" 
         Binding="{Binding Uid}"/> 
    <DataGridTextColumn Width="*" 
         Header="Type" 
         Binding="{Binding Type}"/> 
    <DataGridTextColumn Width="*" 
         Header="ChannelType" 
         Binding="{Binding ChannelType}"/> 

    <DataGridTemplateColumn Width="*"> 

    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <Button Name="btnSend" Click="btnSend_Click">Send</Button> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

이 단추를 클릭하면 "보내기"텍스트가 "취소"로 변경되어야합니다. 어떻게해야합니까?

private void btnSend_Click(object sender, RoutedEventArgs e) 
{ 
    //If I click first button, only first button should be changed 
    //from "Send" to "Cancel" 
    //Rest button should remain as "Send" 
} 

답변

0

신속하고 더러운 :

당신은 UI를 수정하는대로 디스패처-대기열에서 명령을 둘 필요가
private void btnSend_Click(object sender, RoutedEventArgs e) 
{ 
    changeBtnText((Button)sender, "Cancel"); 
} 

private void changeBtnText(Button button, String text) 
{ 
    if (Button.Dispatcher.CheckAccess()) 
    { 
     button.Content = text; 
    } 
    else 
    { 
     Button.Dispatcher.BeginInvoke(()=> 
     { 
      changeBtnText(button); 
     }); 
    } 
} 

만 Dispatcher가 할 수있다 느릅 나무.

관련 문제