C# 从列表框更改面板

C# 从列表框更改面板,c#,listbox,panel,C#,Listbox,Panel,编辑:我解决了这个问题。我们把:在案例和价值之间,它需要在后面。示例:案例“一般”: 感谢您的时间和输入每个人 原件: 这必须比我想做的简单得多。简单地说,我有一个列表框,上面有6个不同的“项目”(General/oSnaps/blah/blah/blah/blah),当有人单击列表框中的一个项目时,我希望它更改面板。我有6个不同的面板(panel1、panel2等)相互叠放在一起,只有第一个面板设置为可见 我确信这只是设置哪个面板可见的问题,但我如何将其链接到列表框中的“项目”呢?我认为Sel

编辑:我解决了这个问题。我们把:在案例和价值之间,它需要在后面。示例:案例“一般”:

感谢您的时间和输入每个人

原件:

这必须比我想做的简单得多。简单地说,我有一个列表框,上面有6个不同的“项目”(General/oSnaps/blah/blah/blah/blah),当有人单击列表框中的一个项目时,我希望它更改面板。我有6个不同的面板(panel1、panel2等)相互叠放在一起,只有第一个面板设置为可见

我确信这只是设置哪个面板可见的问题,但我如何将其链接到列表框中的“项目”呢?我认为SelectedIndexChanged是答案,但我对编程非常陌生

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

任何帮助都将不胜感激

编辑:这是我当前的代码

私有无效列表框1\u SelectedIndexChanged(对象发送方,System.EventArgs e) {

单击列表框中的不同项目确实会显示一个包含正确内容的消息框(常规/E-Snaps/blah/etc)。显然,它正在阅读,我们现在如何使其切换到面板

难道我不能做这样的事情吗

私有无效列表框1\u SelectedIndexChanged(对象发送方,System.EventArgs e) {

}


并不是说我可以让这两种方法都起作用……但这似乎是合乎逻辑的。如果我可以进行某种形式的字符串/布尔转换…

您可以在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;
    }
}
等等,我希望这能有所帮助

说明:“废话”是你想要评估的价值,如果我在做水果的话

“废话”将=苹果
或者bannana或菠萝等

我会在绑定中使用IValueConverter——这将使您可以将每个面板的可见性直接绑定到
列表框。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>

或者,请注意,如果要使用过渡(如交叉淡入淡出、滑入/滑出等),然后您可能希望在
SelectionChanged
事件上使用事件触发器,以及情节提要或VisualState更改。

那么…您是说我希望blah=当前选择的列表框项目?是的,基本上就是这样。是的…我该如何实现?我想我很困惑,因为我似乎没有可以调用列表框中的每个不同项目。我希望能够获取listboxitem1或listboxitem2等。将“blah”更改为我实际拥有的内容(General/oSnaps/etc)没有任何作用。在curItem=listbox1.selecteditem.tostring之后确定放入messagebox.show(curItem)让我知道这是因为每个案例都必须是唯一的,所以case:“General”case:“oSnap”case:“NextOne”case:“TheOneAfter”OK,还要确保比较的是文本而不是值,否则就必须使用“SelectedValue.ToString()“谢谢你的代码,不过这对我来说太高级了。如果我有这个名声,我会投你一票:(
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;
    }
}
<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>
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();
    }
}