【发布时间】:2018-09-26 18:02:48
【问题描述】:
大家下午好,我的问题是这样的。我试图在单击 DataTemplate 中的 TextBox 后触发一个事件,谁能告诉我为什么它没有触发该事件?
按照下面的代码 XAML。
<StackPanel Orientation="Horizontal">
<ItemsControl Grid.Row="0" ItemsSource="{Binding Pagamentos}" HorizontalAlignment="Left" x:Name="cbxFormaDePagamento" Margin="0,0,0,8">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox x:Name="txtFormaPagamento" Text="{Binding FormaPagamento.Nome}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Width="216" Height="45" Background="White" BorderThickness="1" BorderBrush="#b7b7b7" IsEnabled="False" FontFamily="Roboto" FontSize="18" Foreground="Black" Padding="0,0,0,0" Margin="0,0,0,8" MouseLeftButtonDown="txtFormaPagamento_MouseLeftButtonDown"></TextBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
MouseLeftButtonDown 函数
private void txtFormaPagamento_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var viewModel = DataContext as FormasPagamentoViewModel;
foreach (var currencyTextBox in FindVisualChildren<CurrencyTextBox>(this))
{
if (currencyTextBox.Name == "cbxValor")
{
currencyTextBox.Number = viewModel.TotalPagar;
}
}
}
谢谢!
【问题讨论】:
-
当 IsEnabled 属性设置为 True 时,不会触发鼠标事件。你为什么使用禁用的文本框?