简体   繁体   English

将“开关”从Windows窗体更改为WPF

[英]Changing a “switch” from windows form to WPF

We figured out yesterday how to get a listbox's contents to switch between different panels I had stacked on top of each other. 昨天我们弄清楚了如何获取列表框的内容,以便在我堆叠在一起的不同面板之间切换。 I'm attempting to do the same thing in WPF this time, obviously the syntax is different. 这次我试图在WPF中执行相同的操作,显然语法是不同的。 The code worked 100% correctly in the windows form. 该代码在Windows窗体中可以100%正确地工作。 I've tried a few different ways to try to get the now "grids" to show, but to no avail. 我尝试了几种不同的方法来尝试显示现在的“网格”,但无济于事。
Thanks in advance! 提前致谢!

Current code 'attempt'. 当前代码“尝试”。 I'm just demonstrating a couple ways I've attempted to change the code there in that first "case". 我只是展示了几种尝试在第一个“案例”中更改代码的方法。

    private void listBox1_SelectedIndexChanged(object sender, SelectionChangedEventArgs e)
    {

        // set the listboxselected item to a string variable
        string curItem = listBox1.SelectedItem.ToString();
        curItem = listBox1.SelectedItem.ToString();

        // variable changes depening on mouse click, sets to whichever string value is selected
        switch (curItem)
        {
            case "General":
                gridGeneral.Visibility == true;
                gridRightClick.Visibility = Visibility.Visible;
                gridSnaps.Visibility = Visibility.Hidden;
                break;
            case "E-Snaps":
                gridGeneral.Visibility = Visibility.Hidden;
                gridRightClick.Visibility = Visibility.Hidden;
                gridSnaps.Visibility = Visibility.Visible;
                break;
            case "Mouse":
                gridGeneral.Visibility = Visibility.Hidden;
                gridRightClick.Visibility = Visibility.Visible;
                gridSnaps.Visibility = Visibility.Hidden;
                break;

        }

Here is the code that works in a windows form 这是在Windows窗体中工作的代码

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { 私人无效listBox1_SelectedIndexChanged(对象发送者,System.EventArgs e){

           // set the listboxselected item to a string variable
           string curItem = listBox1.SelectedItem.ToString();
           curItem = listBox1.SelectedItem.ToString();

           // variable changes depening on mouse click, sets to whichever string value is selected
           switch(curItem)
        {
               case "General" :
            panel1.Visible = false;
            panel2.Visible = true;
            panel3.Visible = false;
            panel4.Visible = false;
            panel5.Visible = false;
            break;

etc etc etc.... 等等等等...

You wouldn't do this the same way in WPF that you would in WinForms 在WPF中,您不会以与WinForms中相同的方式进行操作

In WPF, you'll probably have a single control in WPF where in WinForms you have 3, and the Template that control uses to render will change based on the SelectedItem of your ListBox 在WPF中,您可能在WPF中只有一个控件,而在WinForms中您只有3个控件,并且控件用于呈现的模板将根据ListBox的SelectedItem进行更改

Most likely the control definition will look something like this, so that the content of it is bound to the ListBox.SelectedItem : 控件定义很可能看起来像这样,以便其内容绑定到ListBox.SelectedItem

<ContentControl Content="{Binding ElementName=listBox1, Path=SelectedItem}" />

And you can either use ContentTemplates or DataTemplates to tell WPF how to draw that ContentControl's ContentTemplate. 您可以使用ContentTemplatesDataTemplates告诉WPF如何绘制ContentControl的ContentTemplate。

If the SelectedItem is a custom class, a DataTemplate would probably be easier, however since its a string in your example, a ContentTemplate is probably better. 如果SelectedItem是自定义类,则DataTemplate可能会更容易,但是由于在示例中为String,因此ContentTemplate可能更好。

Here's an example of a style for that ContentControl which changes the ContentTemplate property based on the value of the Content 这是该ContentControl的样式示例,该样式根据Content的值更改ContentTemplate属性

 <Style TargetType="{x:Type ContentControl}">
    <!-- // Default Template -->
    <Setter Property="ContentTemplate" Value="{StaticResource GeneralTemplate}" />

    <!-- // Change template depending on a property -->
     <Style.Triggers>
         <Trigger Property="Content" Value="ESnaps">
             <Setter Property="ContentTemplate" Value="{StaticResource ESnapsTemplate}" />
         </Trigger>
         <Trigger Property="Content" Value="Mouse">
             <Setter Property="ContentTemplate" Value="{StaticResource MouseTemplate}" />
         </Trigger>
     </Style.Triggers>
 </Style>

(I may have the syntax of the exact binding you need wrong here... will probably need some testing) (我可能具有您在这里需要错误的确切绑定的语法……可能需要进行一些测试)

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

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