WPFリボン、ribbontabが選択されたときにメインのコンテンツを変更
質問
私は、リボンタブをクリックしたときにWPFアプリケーション内の主表面(リボン自体以下のもの)の内容を変更したいと思います。私はそれが非常に問題である、オフィスのリボンを使用していません。だから、これはWPFコンテナコントロールを使うべき、と私はそれをどのように行うのでしょうか?私はちょうど視界に隠れ、または何で種々の制御を持っている必要があります。私は、WPFの専門家ではないので、私は少しのインスピレーションを必要とします。
解決
イリノイ序文ます。
このRibbonTabは予告IsSelectedのための私のスタイルは、
ビューモデルでIsSelectedにバインドされています <!-- RibbonTab -->
<Style TargetType="{x:Type ribbon:RibbonTab}">
<Setter Property="ContextualTabGroupHeader" Value="{Binding ContextualTabGroupHeader}" />
<Setter Property="Header" Value="{Binding Header}" />
<Setter Property="ItemsSource" Value="{Binding GroupDataCollection}" />
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
これは、ビューモデルコード
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (_isSelected != value)
{
_isSelected = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsSelected"));
}
}
}
private bool _isSelected;
TabViewModelのコンストラクタでは、私はコンテンツのViewModelにのためのパラメータを取る。
public TabData(ISelectedContentTab content)
: this(content.DisplayName)
{
_selectedContent = content;
}
private ISelectedContentTab _selectedContent;
は、その後、私は私のXAMLで選択したコンテンツを表示するためのItemsControlを使用
<ItemsControl Grid.Row="1" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding ElementName=ribbon,Path=SelectedItems}"
ItemTemplate="{StaticResource ContentControlTemplate}" />
そして、私が持っているContentControlTemplateがある
<DataTemplate x:Key="ContentControlTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" VerticalAlignment="Stretch" Height="Auto" VerticalContentAlignment="Stretch" Content="{Binding SelectedContent}" />
</Grid>
</DataTemplate>
また、あなたは、ビューにあなたのコンテンツを指しているのDataTemplateを持っていることを確認してください。
この情報がお役に立てば幸いです。
他のヒント
アイデアは(Photoshopやその他のグラフィカルエディタのように)に積層リボン、以下のコンテンツを持つことであり、唯一のあなたはこの瞬間を必要とする層を示しています。ただ、目的のタブのプロパティをVisibility
ためにあなたの層のIsSelected
をバインド
ここでMainGrid(すぎるグリッドである)層のためのコンテナである
<Grid x:Name="MainGrid">
<Grid Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=ribbonTab2}">
<Image x:Name="ImgMain" Source="x.jpg"/>
</Grid>
<Grid Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=ribbonTab1}">
<Image x:Name="ImgXtra" Source="y.jpg"/>
</Grid>
</Grid>
...そして、あなたはまったくコードは必要ありません!
P.S。ああ、私はあなたが当然のリソースにBooleanToVisibilityConverter
を宣言する必要が忘れ
私はそれを行うために、より簡単な方法があると思います。私はこのようにそれを行っている。
<Frame NavigationUIVisibility="Hidden" x:Name="FrmMainFrame" DockPanel.Dock="Bottom"/>
そして背後にあるコードでます:
mainWindowView.RibMain.SelectionChanged += RibMain_SelectionChanged;
void RibMain_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var tab = this.mainWindowView.RibMain.SelectedItem as RibbonTab;
if (tab.Header.Equals("Explorer"))
{
mainWindowView.FrmMainFrame.Navigate(explorerController.View());
}
else
mainWindowView.FrmMainFrame.NavigationService.Navigate(new Uri("http://www.google.com/"));
}
私は、これは古いスレッドですけど、私はこれで問題を抱えていると、すべてのvb.netの助けを発見していないが、私は自分で解決策を発見した...ので、ここでそれは次のとおりです。
あなたが背後にあるコードでそれを扱うことができるように、あなたのRibbonTabの名前を付けます。 私は、ビューとコントロールを追加するための複数の方法があります知っているが、ここで私がやったことです... 私は単にリボン後の主グリッド内の私の見解のための新しいグリッドを追加しました。すなわちます:
<r:RibbonWindow>
<Grid>
<r:Ribbon>
<r:RibbonTab Name="Tab1" Header="Home">
<r:RibbonGroup Name="Group1">
<r:RibbonButton LargeImageSource="images\icon.png" Label="Click Me"/>
</r:RibbonGroup>
</r:RibbonTab>
<r:RibbonTab Name="Tab2" Header="Other Tab">
<r:RibbonGroup Name="Group2">
<r:RibbonButton LargeImageSource="images\icon.png" Label="Click Me"/>
</r:RibbonGroup>
</r:RibbonTab>
</r:Ribbon>
<Grid Name="Tab1RTB" Grid.Row="1" Visibility="Hidden">
<RichTextBox Margin="5" BorderBrush="LightGray" BorderThickness="1"/>
</Grid>
<Grid Name="Tab2RTB" Grid.Row="1" Visibility="Hidden">
<RichTextBox Margin="5" BorderBrush="LightGray" BorderThickness="1"/>
</Grid>
</Grid>
</r:RibbonWindow>
の後ろに、コード(VB.NET)
Private Sub TabChanged(sender As System.Object, e As SelectionChangedEventArgs) Handles ribbonHome.SelectionChanged
If Tab1.IsSelected = True Then
Tab1RTB.Visibility = Windows.Visibility.Visible
Tab2RTB.Visibility = Windows.Visibility.Collapsed
ElseIf Tab2.IsSelected = True
Tab1RTB.Visibility = Windows.Visibility.Collapsed
Tab2RTB.Visibility = Windows.Visibility.Visible
Else
Tab1RTB.Visibility = Windows.Visibility.Collapsed
Tab2RTB.Visibility = Windows.Visibility.Collapsed
End If
End Sub