【发布时间】: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)的基类,然后是每个
ViewModel的DataTemplate。绑定:Combobox.SourceItems到ObservableCollection<BaseViewModel>,Combobox.SelectedItem到Frame.Source(可能通过转换器)。 -
如果您只想使用 xaml 来执行此操作,可以在 ComboBox 上使用 DataTriggers 将 ChangePropertyAction 触发到框架的源作为 TargetProperty。
标签: c# wpf xaml combobox frame