简体   繁体   English

如何在WPF C#中显示DataGrid按钮上的弹出窗口?

[英]How to show a popup on datagrid button click in wpf c#?

I have a common popup in my wpf window and I have a button in the datagrid as well. 我在wpf窗口中有一个普通的弹出窗口,在datagrid中也有一个按钮。 What I want is to open the popup when the button is clicked. 我想要的是单击按钮时打开弹出窗口。 The popup should be independent to each button click. 弹出窗口应独立于每次单击按钮。 For example, let's say that I have two rows in the datagrid. 例如,假设我在数据网格中有两行。 When I click on the first button popup should appear,then I do some changes to that popup and close it. 当我单击第一个按钮时,应该会出现弹出窗口,然后我对该弹出窗口进行一些更改并将其关闭。 Now I click the second button it should open a new popup instead of the changes I made before. 现在,我单击第二个按钮,它应该打开一个新的弹出窗口,而不是我之前所做的更改。 I'm using a common popup for this. 我为此使用了一个通用弹出窗口。 Please anyone tell me is it possible to handle my requirement with common popup window? 请任何人告诉我可以通过通用弹出窗口处理我的要求吗?

XAML XAML

<Popup x:Name="popUpServer" IsOpen="False" Placement="MousePoint" >
                        <Border Background="#FFEFF2F3" BorderBrush="Black" BorderThickness="1" Width="229" Height="145">
                            <StackPanel  Orientation="Horizontal" Margin="0,0,0,-17">
                                <Grid Width="227" Margin="0,0,0,10">
                                    <GroupBox Header="Configuration" HorizontalAlignment="Left" Margin="9,6,-9,0" VerticalAlignment="Top" Height="125" Width="211">
                                        <Grid>
                                            <Label Content="Auto Restart" HorizontalAlignment="Left" Margin="10,6,0,0" VerticalAlignment="Top"/>
                                            <ToggleSwitch:HorizontalToggleSwitch  x:Name="tsAutoRestart" HorizontalAlignment="Left" Margin="97,10,0,0" VerticalAlignment="Top" Width="46" ThumbSize="22" Height="21" RenderTransformOrigin="3.522,1.048">
                                                <ToggleSwitch:HorizontalToggleSwitch.UncheckedBackground>
                                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                        <GradientStop Color="#FFC80000" Offset="1"/>
                                                        <GradientStop Color="#FF0A0A0A" Offset="0.853"/>
                                                    </LinearGradientBrush>
                                                </ToggleSwitch:HorizontalToggleSwitch.UncheckedBackground>
                                                <ToggleSwitch:HorizontalToggleSwitch.CheckedBackground>
                                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                        <GradientStop Color="#000000" />
                                                        <GradientStop Color="#000000" Offset="1" />
                                                    </LinearGradientBrush>
                                                </ToggleSwitch:HorizontalToggleSwitch.CheckedBackground>
                                                <ToggleSwitch:HorizontalToggleSwitch.ThumbBrush>
                                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                        <GradientStop Color="#FFD6D4D4" />
                                                        <GradientStop Color="#FFD6D4D4" Offset="1" />
                                                        <GradientStop Color="#FFD6D4D4" Offset="0.02" />
                                                    </LinearGradientBrush>
                                                </ToggleSwitch:HorizontalToggleSwitch.ThumbBrush>
                                            </ToggleSwitch:HorizontalToggleSwitch>
                                            <ComboBox x:Name="cbDuration" HorizontalAlignment="Left" VerticalAlignment="Top" Width="92" Margin="97,40,0,0">
                                                <ComboBoxItem Content="30 Minutes"/>
                                                <ComboBoxItem Content="1 Hours"/>
                                                <ComboBoxItem Content="2 Hours"/>
                                            </ComboBox>
                                            <Label Content="After" HorizontalAlignment="Left" VerticalAlignment="Top" Width="83" Margin="9,36,0,0"/>
                                            <Button x:Name="btnApply" Content="Apply" HorizontalAlignment="Left" Margin="125,75,0,0" VerticalAlignment="Top" Width="64" Click="BtnApply_Click"/>
                                        </Grid>
                                    </GroupBox>

                                </Grid>
                            </StackPanel>
                        </Border>
                    </Popup>

Datagrid 数据网格

<DataGridTemplateColumn>
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <Button x:Name="txtServerInfo" Click="TxtServerInfo_Click" Height="23" Width="28">
                                                    <Button.Background>
                                                        <ImageBrush ImageSource="img.png"/>
                                                    </Button.Background>
                                                </Button>
                                            </StackPanel>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

Code behind file 文件后面的代码

 private void TxtServerInfo_Click(object sender, RoutedEventArgs e)
        {
            popUpServer.IsOpen = true;
        }

如果要使用公共弹出窗口,则最好在单独的用户控件中创建一个弹出窗口。然后单击按钮可以为该用户控件创建一个新对象,然后使用该用户控件对象打开该弹出窗口。弹出窗口将与上一次单击按钮无关。

please try the next solution; 请尝试下一个解决方案; combine your popup with the button into a separate user control, in that way you can trigger the popup opening based on the button click, and for each button click there will be it's(button) own popup opened there. 将您的弹出窗口和按钮组合到一个单独的用户控件中,这样您就可以基于按钮的单击触发弹出窗口的打开,并且对于每个按钮单击,都会在那里打开(按钮)自己的弹出窗口。 When you combine the popup and the button together they will have the same data context, thus you will have no problem to make a binding, from the other hand if you want a different data contexts for these controls you can support the user with two dependency properties form that a combined user control. 当您将弹出窗口和按钮组合在一起时,它们将具有相同的数据上下文,因此进行绑定没有问题,另一方面,如果您希望这些控件具有不同的数据上下文,则可以为用户提供两个依赖属性形式表示组合的用户控件。

I'll glad to help if you will have problems with the code, let me know about that. 如果您的代码有问题,我们将很乐意为您提供帮助,请让我知道。 Regards. 问候。

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

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