在后面的代码中访问DataTemplate控件 [英] Access DataTemplate controls in code behind

查看:59
本文介绍了在后面的代码中访问DataTemplate控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此代码有疑问:

<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 点击事件我无法访问弹出窗口以关闭它并对其进行其他更改。

In code behind in btnClosePopup click event I can't access to popup to close it and do some other changes on it.

使用 FindName()方法,但不适用于我

I have tried to use FindName() method but it doesn't work for me

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

请提供帮助并告诉我如何在后面的代码中访问DataTemplate内部的控件吗? / p>

Please can you help and tell me how do I access to controls that inside DataTemplate in code behind?

推荐答案

您已经打开/关闭 Popup 在此行:

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

作为@dkozl的替代答案,您可以关闭 Popup 这样的方式:

As an alternative answer from @dkozl, you can close the Popup in such a way:

<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>

或者您可以直接指定属性 IsOpen 弹出窗口的数目:

Or you can directly specify a property IsOpen of Popup:

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

但是在这种情况下,背景颜色为 Button 的状态为 IsChecked = True 。为了避免这种情况,无需为控件创建新的模板,就可以使用扁平按钮的系统样式:

But in this case at the background color of Button will be in state of IsChecked="True". To avoid this, without creating a new Template for your Control, you can use a system style of flat button:

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

这篇关于在后面的代码中访问DataTemplate控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆