wpf - 如何在选择列表框中的项目时打开新的用户控件?

标签 wpf data-binding mvvm listboxitem

我是 MVVM 的新手。我有一个主项目“MVVM Demo”,其中包括 mainwindow.xaml 文件,它有我的主 UI。我有一个包含一组项目的列表框:

<ListBox Name="ButtonPanel" ItemsSource="{Binding BoardTabs}" SelectedItem="{Binding SelectedTab, Mode=TwoWay}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Connect}"  >
                            </TextBlock>
                            <TextBlock Text="{Binding I2C}"  >
                            </TextBlock>
                            <TextBlock Text="{Binding Voltage}" >
                            </TextBlock>
                            <TextBlock Text="{Binding Clock}" >
                            </TextBlock>
                            <TextBlock Text="{Binding Codec}" >
                            </TextBlock>
                            <TextBlock Text="{Binding EEPROM}" >
                            </TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我需要列表框的每个项目分别打开一个新的用户控件。请注意,我在主项目(我的解决方案文件中的第二个项目)之外创建了一个单独的用户控件,并将其添加为对主项目的引用(板控制 mvvm)

Voltage 是 userControl 之一,它有一个 voltage.xaml 文件,我的 Ui 所在的文件有一个单独的 View 、模型和 View 模型类。当我单击位于 Board Control MVVM 项目中的 MainWindow.xaml 中的电压项(列表框中的文本 block )时,它会弹出一个新窗口。

这是我的 ProductView 模型类,它从 mainwindow.xaml UI 中选择选项卡:
private Product m_SelectedTab;
    public Product SelectedTab
    {
        get
        {
            return m_SelectedTab;
        }

        set
        {
            m_SelectedTab = value;
            NotifyPropertyChanged("SelectedTab");
        }
    }

我想知道当我在 Listbox(mainwindow.xaml) 中选择项目时,会弹出一个窗口,上面有一个 voltage.xaml UI。如果您需要任何进一步的代码以进行清晰的描述,我很乐意在这里上传。

请帮忙 :(

最佳答案

只是要清楚;如果 Connect、I2C、Voltage 等是您想要在 ListBox 中的项目,那么上面的 Xaml 不是您想要的。因为每个项目都将具有所有这些 TextBlocks(因为您将 ItemTemplate 定义为几个 TextBlocks)。

相反,我假设您想要的是这些项目(连接、I2C 等)中的每一个都是您的 ListBox 的项目。再次假设您要做的唯一事情是打开另一个页面,然后您可以在每个 ListBoxItem 内使用 HyperlinkBut​​ton:

<ListBox x:Name="ListBoxItems" ItemsSource="{Binding Path=Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>                        
                    <HyperlinkButton NavigateUri="{Binding Path=Page}">
                        <HyperlinkButton.Style>
                            <Style TargetType="HyperlinkButton">
                                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="HyperlinkButton">
                                            <ContentPresenter/>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </HyperlinkButton.Style>
                        <TextBlock Text="{Binding Path=Name}" />                            
                    </HyperlinkButton>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

这里 Name 和 Page 是 Item 类的属性,它表示您的每个项目(Connect、I2C 等),其中 Name 是名称(电压),Page 是 Uri,它指向您要打开的相应页面(/Voltage. xml)

ListBox 的 ItemsSource 绑定(bind)到 ViewModel 类的 Items 属性,其中 Items 是 Item 对象的 ObservableCollection:
ItemsViewModel viewModel = new ItemsViewModel() {
            Items = new ObservableCollection<Item>() { 
            new Item() { Name = "Item 1", Page = new Uri("/Page1.xaml", UriKind.Relative) },
            new Item() { Name = "Item 2", Page = new Uri("/Page2.xaml", UriKind.Relative) }}
        };

请注意,如果您想向页面传递额外的数据,您可以通过查询字符串 (/Voltage.xaml?param=blah) 执行此操作,或者您可以向每个项目添加命令并自己处理导航。

关于wpf - 如何在选择列表框中的项目时打开新的用户控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351326/

相关文章:

wpf - 如果返回的值为 Null,如何使 PriorityBinding 失败?

c# - 访问 WPF 中列表框内的复选框

android - 无法使用数据绑定(bind)在包含的布局中传递颜色变量

c# - 组合多个列表

mvvm - 如何在一个窗口中创建 VM 之间的 MVVM 父/子关系?

wpf - 如何在其中使用 Binding 和 Binding

WPF UI 元素上的调度程序对象发生了什么?

data-binding - Polymerfire 查询 orderByChild 不工作

javascript - 在 MVVM modelView 中进行初始化有意义吗?

c# - 避免在每个 setter 中调用 RaisePropertyChanged