【问题标题】:Access DataTemplate controls in code behind在后面的代码中访问 DataTemplate 控件
【发布时间】:2014-03-29 13:24:05
【问题描述】:

我对这段代码有疑问:

<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 click 事件后面的代码中,我无法访问弹出窗口来关闭它并对其进行一些其他更改。

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

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

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

【问题讨论】:

    标签: c# wpf datatemplate


    【解决方案1】:

    您不必在后面的代码中执行此操作,如果您在代码中更改 Popup.IsOpen,它将不会再次出现,因为您将失去绑定。您需要将ToggleButton 上的IsChecked 设置为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>
    

    【讨论】:

    • 感谢@dkozl 抽出宝贵时间
    【解决方案2】:

    你已经在这行Open/Close这个Popup

    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" 的状态。为避免这种情况,无需为您的控件创建新模板,您可以使用平面按钮的系统样式:

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

    【讨论】:

    • 谢谢@Anatoliy Nikolaev
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2011-04-17
    • 2011-09-05
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多