質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

15741閲覧

C#,WPFのStackPanelのVisibilityの切り替えをTabItemsのIsSelectedプロパティで紐づける方法がうまくいかない

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2016/09/19 12:51

###前提・実現したいこと
お世話になります。
C#のWPFにて、TabControlの切り替えによりStackPanelの表示を切り替える処理を行っております。

###発生している問題・エラーメッセージ
現在、下記のコードで、tab1選択時にはStackPanelのPnl1を、tab2選択時にはStackPanelのPnl2を表示しようと考えております。
しかし、常に両方のStackPanelが表示されてしまい、切り替えができません。
しかし、切り替えはDataTriggerにてStackVisibleクラスのstk1Visualプロパティの値がTrueの時にPnl1を表示、Falseの時にはPnl1を非表示にする処理を設定しており、Pnl2についてもstk2Visualプロパティにて同様の設定を行っております。
なお、TabItemsの中にStackPanelを入れることはできません。
よろしくお願いいたします。

###該当のソースコード
(MainWindow.xaml)
<Window x:Class="TabTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TabTest" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:StackVisible x:Key="stackVisible"/>

<Style x:Key="style1" TargetType="StackPanel"> <Style.Triggers> <DataTrigger Binding="{Binding stk1visual}" Value="True"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> <DataTrigger Binding="{Binding stk1visual}" Value="False"> <Setter Property="Visibility" Value="Collapsed"/> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="style2" TargetType="StackPanel"> <Style.Triggers> <DataTrigger Binding="{Binding stk2visual}" Value="True"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> <DataTrigger Binding="{Binding stk2visual}" Value="False"> <Setter Property="Visibility" Value="Collapsed"/> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <StackPanel> <TabControl Name="maintab"> <TabItem Header="Tab1" Height="30" Width="50" Name="tab1" DataContext="{StaticResource stackVisible}" IsSelected="{Binding stk1visual}"> </TabItem> <TabItem Header="Tab2" Height="30" Width="50" Name="tab2" DataContext="{StaticResource stackVisible}" IsSelected="{Binding stk2visual}"> </TabItem> </TabControl> </StackPanel> <StackPanel Name="Pnl1" Style="{StaticResource style1}"> <Label Content="Label1"/> </StackPanel> <StackPanel Name="Pnl2" Style="{StaticResource style2}"> <Label Content="Label2"/> </StackPanel> </StackPanel>
</Window>

(MainWindow.xaml.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TabTest
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}

public class StackVisible { public bool stk1visual { get; set; } public bool stk2visual { get; set; } }

}

###試したこと
TabItemをRadioButtonにかえて、IsCheckedプロパティにて行ったが、結果は変わらなかった。

###補足情報(言語/FW/ツール等のバージョンなど)
開発環境:Visual C# 2015 Community
Windows 8 64bit

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

タブのIsSelectedプロパティとStackPanelのVisibilityプロパティのような型の異なる値をBindingしたい場合のために値コンバーターという手段が用意されています。

C#

1//新しく用意する値コンバータークラス 2 public class BoolVisibilityConverter : IValueConverter 3 { 4 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 5 { 6 return ((bool?)value == true) ? Visibility.Visible : Visibility.Collapsed; 7 } 8 9 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 10 { 11 throw new NotImplementedException();//Visibility→Bool変換は不要なため未実装 12 } 13 }

XAMLもシンプルになります。

XML

1<Window x:Class="TabTest.MainWindow" 2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6xmlns:local="clr-namespace:TabTest" 7mc:Ignorable="d" 8Title="MainWindow" Height="350" Width="525"> 9 <Window.Resources> 10 <local:BoolVisibilityConverter x:Key="BoolVisibilityConverter" /> 11 </Window.Resources> 12 <StackPanel> 13 <StackPanel> 14 <TabControl Name="maintab"> 15 <TabItem Header="Tab1" Height="30" Width="50" Name="tab1" /> 16 <TabItem Header="Tab2" Height="30" Width="50" Name="tab2" /> 17 </TabControl> 18 </StackPanel> 19 <StackPanel Name="Pnl1" Visibility="{Binding ElementName=tab1, Path=IsSelected, Converter={StaticResource BoolVisibilityConverter}}"> 20 <Label Content="Label1"/> 21 </StackPanel> 22 <StackPanel Name="Pnl2" Visibility="{Binding ElementName=tab2, Path=IsSelected, Converter={StaticResource BoolVisibilityConverter}}"> 23 <Label Content="Label2"/> 24 </StackPanel> 25 </StackPanel> 26</Window>

StackVisible クラスは不要になります。

投稿2016/09/19 13:50

nakit

総合スコア410

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2016/09/19 14:42

ありがとうございます。 こんなに複雑にする必要はなかったのですね。 参考にさせていただきます。
guest

0

ベストアンサー

こんにちは。

とりあえず提示コードでは

  • 変更通知が発生してなさそう
  • スタイル適用対象のStackPanelが静的インスタンスをそもそもバインドしてない

なので、以下のようにしてみてください。

C#

1public class StackVisible : INotifyPropertyChanged 2{ 3 private bool _stk1visual; 4 public bool stk1visual 5 { 6 get { return _stk1visual; } 7 set { 8 _stk1visual = value; 9 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(stk1visual))); 10 } 11 } 12 13 private bool _stk2visual; 14 public bool stk2visual 15 { 16 get { return _stk2visual; } 17 set 18 { 19 _stk2visual = value; 20 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(stk2visual))); 21 } 22 } 23 24 public event PropertyChangedEventHandler PropertyChanged; 25} 26

XML

1<StackPanel Name="Pnl1" Style="{StaticResource style1}" DataContext="{StaticResource stackVisible}"> 2 <Label Content="Label1"/> 3</StackPanel> 4<StackPanel Name="Pnl2" Style="{StaticResource style2}" DataContext="{StaticResource stackVisible}"> 5 <Label Content="Label2"/> 6</StackPanel>

投稿2016/09/19 13:15

Tak1wa

総合スコア4791

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2016/09/19 13:30

ありがとうございました。 StackPanelにもDataContextにてバインドする必要があったのですね。 参考にさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問