I have problem with this code:
我有这个代码的问题:
<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>
In code behind in btnClosePopup
click event I can't access to popup to close it and do some other changes on it.
在btnClosePopup单击事件后面的代码中,我无法访问弹出窗口以关闭它并对其进行一些其他更改。
I have tried to use FindName()
method but it doesn't work for me
我试过使用FindName()方法,但它对我不起作用
var template = lbInvoice.Template;
var myControl = (Popup)template.FindName("popQuantity", lbInvoice);
Please can you help and tell me how do I access to controls that inside DataTemplate in code behind?
请问您能帮忙告诉我如何在代码中访问DataTemplate内部的控件?
2 个解决方案
#1
1
You have already to Open/Close
this Popup
in this line:
您已经在此行中打开/关闭此弹出窗口:
IsOpen="{Binding IsChecked, ElementName=btnInvoiceQuantity}"
As an alternative answer from @dkozl, you can close the Popup
in such a way:
作为@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>
Or you can directly specify a property IsOpen
of Popup:
或者您可以直接指定Popup的属性IsOpen:
<ToggleButton x:Name="btnClosePopup"
IsChecked="{Binding Path=IsOpen, ElementName=popQuantity}" ... />
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:
但在这种情况下,Button的背景颜色将处于IsChecked =“True”的状态。为避免这种情况,无需为Control创建新模板,您可以使用系统样式的平面按钮:
<ToggleButton x:Name="btnClosePopup"
Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}" ... />
#2
2
You don't have to do it in code behind and if you change Popup.IsOpen
in code it won't appear again as you'll lose you binding. You need to set IsChecked
on ToggleButton
to false and you can do it with EventTrigger
您不必在后面的代码中执行此操作,如果您在代码中更改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>
#1
1
You have already to Open/Close
this Popup
in this line:
您已经在此行中打开/关闭此弹出窗口:
IsOpen="{Binding IsChecked, ElementName=btnInvoiceQuantity}"
As an alternative answer from @dkozl, you can close the Popup
in such a way:
作为@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>
Or you can directly specify a property IsOpen
of Popup:
或者您可以直接指定Popup的属性IsOpen:
<ToggleButton x:Name="btnClosePopup"
IsChecked="{Binding Path=IsOpen, ElementName=popQuantity}" ... />
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:
但在这种情况下,Button的背景颜色将处于IsChecked =“True”的状态。为避免这种情况,无需为Control创建新模板,您可以使用系统样式的平面按钮:
<ToggleButton x:Name="btnClosePopup"
Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}" ... />
#2
2
You don't have to do it in code behind and if you change Popup.IsOpen
in code it won't appear again as you'll lose you binding. You need to set IsChecked
on ToggleButton
to false and you can do it with EventTrigger
您不必在后面的代码中执行此操作,如果您在代码中更改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>