繁体   English   中英

面板从列表框更改

[英]Panel change from ListBox

编辑编辑:我发现了问题。 我们将:放在大小写和值之间,需要放在后面。 例如:case“ General”:

感谢您的时间,并输入大家。

原版的:

这必须比我尝试做的要简单得多。 简单来说,我有一个带有说... 6个不同“项目”的列表框(常规/ oSnaps / blah / blah / blah),当有人单击列表框中的一个项目时,我希望它更改面板。 我有6个不同的面板(panel1,panel2等)彼此堆叠,在加载时只有第一个面板可见。

我敢肯定,这只是设置哪个面板可见的问题,但是实际上如何将其链接到列表框中的“项目”? 我想答案是SelectedIndexChanged,但是我对编程非常陌生。

MSDN仅演示如何在不同列表框中的项目之间切换。

任何帮助,将不胜感激。

编辑:这是我当前的代码

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

           string curItem = listBox1.SelectedItem.ToString(); 

           curItem = listBox1.SelectedItem.ToString(); 
            MessageBox.Show(curItem);

           switch(curItem)
        {
        case : "General"   //error here is: Only assignment, call,increment,await,and new object expressions can be used as a statement
        panel1.Visible = false;
        panel2.Visible = true;
        break;
        case : "E-Snaps" // same as above
        panel2.Visible = false;
        panel3.Visible = true;
        break;
        case : "blah" // same as above
        panel3.Visible = false;
        panel4.Visible = true;
        }
        }

单击列表框中的不同项目会弹出一个带有正确内容的消息框(常规/ E-快照/ blah / etc)。 因此,很明显它正在阅读,我们现在如何使它成为开关面板?

我不应该也可以做这样的事情吗?

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

           string curItem = listBox1.SelectedItem.ToString();

           curItem = listBox1.SelectedItem.ToString();

            if curItem = "General"
            {
                panel1.Visible = true;
            }

}

并不是说我可以任其工作……但这似乎合乎逻辑。 如果我可以进行某种形式的字符串/布尔转换...

您可以通过对selectedindex更改后使用switch语句开始

所以看起来像这样

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    // Get the currently selected item in the ListBox. 
    string curItem = listBox1.SelectedItem.ToString();

    switch(curItem)
    {
        case "blah": 
            panel1.visible = false;
            panel2.visible = true;
            break;
        case "blah": 
            panel2.visible = false;
            panel3.visible = true;
            break;
        case "blah": 
            panel3.visible = false;
            panel4.visible = true;
            break;
    }
}

等等,希望对您有所帮助

说明:“ blah”是您想要评估的价值,因此,如果我在做水果

“ blah” =苹果,香蕉或菠萝等

我将在绑定中使用IValueConverter,这将使您可以将每个面板的可见性直接绑定到ListBox.SelectedIndex

<Grid>
    <Grid.Resources>
        <local:IndexToVisibilityConverter x:Key="IndexToVisibilityConverter" />
    </Grid.Resources>
    <ListBox x:Name="listBox"> ... </ListBox>

    <Border x:Name="panel1" 
            Visibility="{Binding ElementName=listBox,
                                 Path=SelectedIndex,
                                 Converter={StaticResource IndexToVisibilityConverter}
                                 ConverterParameter='0'}"
    />
    <Border x:Name="panel2" 
            Visibility="{Binding ElementName=listBox,
                                 Path=SelectedIndex,
                                 Converter={StaticResource IndexToVisibilityConverter}
                                 ConverterParameter='1'}"
    />
</Grid>

如果索引与参数匹配,则“ IndexToVisibilityConverter”应返回“ Visibility.Visible”:

public class IndexToVisibilityConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((int)value == int.Parse((string)parameter) ? Visibility.Visible : Visibility.Collapsed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

另外,请注意,如果要使用过渡(如淡入淡出,滑入/滑出等),则可能需要在SelectionChanged事件上使用事件触发器以及Storyboard或VisualState更改。

暂无
暂无

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

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