简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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