2012-04-03 91 views
0

代碼包含數據網格中的按鈕。每個包含文本「發送」。使用事件處理程序更改按鈕的內容

<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

快速和骯髒的:

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); 
     }); 
    } 
} 

你需要把命令的調度,隊列爲要修改的UI,只至極的分派器允許這樣做。

相關問題