繁体   English   中英

在后面的代码中访问DataTemplate控件

[英]Access DataTemplate controls in code behind

我有这个代码的问题:

<ListBox x:Name="lbInvoice" ItemsSource="{Binding ocItemsinInvoice}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <ToggleButton x:Name="btnInvoiceItem">
                <StackPanel Orientation="Horizontal">
                    <ToggleButton x:Name="btnInvoiceQuantity" Content="{Binding Quantity}"/>
                    <TextBlock Text="{Binding Item.ItemName}" Width="175" Padding="7,5,0,0"/>
                </StackPanel>
            </ToggleButton>
            <Popup x:Name="popQuantity" Closed="popQuantity_Closed" PlacementTarget="{Binding ElementName=btnInvoiceQuantity}" IsOpen="{Binding IsChecked,ElementName=btnInvoiceQuantity}">
                    <Grid>
                        <TextBlock x:Name="tbUnitPrice" Text="Unit Price"/>
                        <Button x:Name="btnClosePopup" Click="btnClosePopup_Click">
                    </Grid>
            </Popup>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

btnClosePopup单击事件后面的代码中,我无法访问弹出窗口来关闭它并对其进行一些其他更改。

我试过使用FindName()方法,但它对我不起作用

var template = lbInvoice.Template;
var myControl = (Popup)template.FindName("popQuantity", lbInvoice);

请问您能帮忙告诉我如何在代码中访问DataTemplate内部的控件?

您不必在后面的代码中执行此操作,如果您在代码中更改Popup.IsOpen ,它将不会再次出现,因为您将失去绑定。 您需要将ToggleButton上的IsChecked ToggleButton为false,并且可以使用EventTrigger执行此操作

<Button Content="Close" x:Name="btnClosePopup">
   <Button.Triggers>
      <EventTrigger RoutedEvent="Button.Click">
         <BeginStoryboard>
            <Storyboard>
               <BooleanAnimationUsingKeyFrames Storyboard.TargetName=" btnInvoiceQuantity" Storyboard.TargetProperty="IsChecked">
                  <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0"/>
               </BooleanAnimationUsingKeyFrames>
            </Storyboard>
         </BeginStoryboard>
      </EventTrigger>
   </Button.Triggers>
</Button>

您已经在此行中Open/ClosePopup

IsOpen="{Binding IsChecked, ElementName=btnInvoiceQuantity}"

作为@dkozl的替代答案,您可以通过以下方式关闭Popup

<Popup x:Name="popQuantity" 
       IsOpen="{Binding Path=IsChecked, ElementName=btnInvoiceQuantity}">

    <Grid Width="200" Height="200" Background="Gainsboro">
        <TextBlock Text="Unit Price" />

        <ToggleButton x:Name="btnClosePopup" 
                      IsChecked="{Binding Path=IsChecked, ElementName=btnInvoiceQuantity}"
                      Content="Close"
                      Width="100" 
                      Height="30" />
    </Grid>
</Popup>

或者您可以直接指定Popup的属性IsOpen

<ToggleButton x:Name="btnClosePopup"
               IsChecked="{Binding Path=IsOpen, ElementName=popQuantity}" ... />

但在这种情况下, Button的背景颜色将处于IsChecked="True" 为避免这种情况,无需为Control创建新模板,您可以使用系统样式的平面按钮:

<ToggleButton x:Name="btnClosePopup"
              Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}" ... />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM