【问题标题】:C# WPF how to change frame source through combobox selection?C# WPF 如何通过组合框选择更改帧源?
【发布时间】:2014-12-01 13:12:15
【问题描述】:

各位程序员,

我的 XAML 中有一个组合框和一个框架。 但是,当组合框列表中的选定值与默认值不同时,我希望能够更改框架的来源。

代码:

XAML

框架

    <DockPanel>
        <Frame x:Name="comboFrame" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Width="Auto" Height="Auto" Source="pan.xaml"/>
    </DockPanel>

组合框

    <ComboBox x:Name="NumberBox" HorizontalAlignment="Left" Height="20" Margin="160,74,0,0" VerticalAlignment="Top" Width="40" SelectionChanged="ComboBox_SelectionChanged_1" ItemsSource="{Binding elements, BindsDirectlyToSource=True, StringFormat=\{0:X\}}" BorderThickness="0">

        <ComboBoxItem Content="3"/>
        <ComboBoxItem Content="4"/>
        <ComboBoxItem Content="5"/>
        <ComboBoxItem Content="6"/>
        <ComboBoxItem Content="7"/>
        <ComboBoxItem Content="8"/>   

    </ComboBox>

C#

    private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)

    {
        
        string stX = Convert.ToString(NumberBox.SelectedItem);

        comboFrame.Navigate(new Uri("pan.xaml", UriKind.Relative));

        if (stX == "3")
        {

            comboFrame.Navigate(new Uri("pan1.xaml", UriKind.Relative));

        }

    }

总之 - 我希望能够更改选择框架的来源,与组合框中的 2 不同。默认源是 pan.xaml,即选择 2。 基本上取决于数量。这些面板都包含其他组合框。 面板包含 2 个 CB(组合框),面板 1 包含 3 个 CB,依此类推,直到面板 6 包含 8 个 CB。

提前致谢。

【问题讨论】:

  • MVVM?我可以想到项目(ViewModels)的基类,然后是每个ViewModelDataTemplate。绑定:Combobox.SourceItemsObservableCollection&lt;BaseViewModel&gt;Combobox.SelectedItemFrame.Source(可能通过转换器)。
  • 如果您只想使用 xaml 来执行此操作,可以在 ComboBox 上使用 DataTriggers 将 ChangePropertyAction 触发到框架的源作为 TargetProperty。

标签: c# wpf xaml combobox frame


【解决方案1】:

我设法解决了。

新代码:

XAML - 为内容添加名称;

    <ComboBox x:Name="NumberBox" HorizontalAlignment="Left" Height="20" Margin="160,74,0,0" VerticalAlignment="Top" Width="40" SelectionChanged="ComboBox_SelectionChanged_1" ItemsSource="{Binding elements, BindsDirectlyToSource=True, StringFormat=\{0:X\}}" BorderThickness="0">

        <ComboBoxItem Name="TwoBoxItem" Content="2"/>
        <ComboBoxItem Name="ThreeBoxItem" Content="3"/>
        <ComboBoxItem Name="FourBoxItem" Content="4"/>
        <ComboBoxItem Name="FiveBoxItem" Content="5"/>
        <ComboBoxItem Name="SixBoxItem" Content="6"/>
        <ComboBoxItem Name="SevenBoxItem" Content="7"/>
        <ComboBoxItem Name="EightBoxItem" Content="8"/>

    </ComboBox>

C# - 使用 XAML 内容名称,检查选择是否等于它,然后使用 dispatcher.invoke 将框架重定向到具有相关组合框数量的页面的 URL。

    private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {

        Uri oneX = new Uri("pan.xaml", UriKind.Relative);

        if (NumberBox.SelectedItem == TwoBoxItem)
        {

            comboFrame.Dispatcher.Invoke(delegate
            {

                comboFrame.Source = oneX;

            }

            );

        }

感谢大家的回答,希望对大家有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多