2017-02-03 9 views
2

私は、ComboBox(ドロップダウンリスト)のみを持つWPFウィンドウを持っています。インデックス1(ドロップダウンリストの2番目の項目)を選択した場合、どのようにWPFウィンドウを拡張して、より多くのボタン、テキストボックスなどを表示できますか? selectedIndexプロパティを使用する必要がありますか?もしそうなら、XAMLでウィンドウをどのように拡張するのですか?特定の条件のWPFウィンドウを拡張する方法は?

答えて

1

私はこれを達成するために過去にIValueConverterを使用しました。これはどういう

public class MyConverter : System.Windows.Data.IValueConverter { 

    public object Convert (object value , Type targetType , object parameter , CultureInfo culture) { 

     if (value == null) 
      return System.Windows.Visibility.Hidden; 

     if (parameter == null) 
      return System.Windows.Visibility.Hidden; 

     if (value.ToString().Equals (parameter)) 
      return System.Windows.Visibility.Visible; 

     return System.Windows.Visibility.Hidden; 
    } 

    public object ConvertBack (object value , Type targetType , object parameter , CultureInfo culture) { 
     throw new NotImplementedException (); 
    } 
} 

が、それはこのような項目コントロールのSelectedIndexをとして、私は数を期待しています、それに渡された値を取ります。ここではサンプル変換器です。次に、渡されたパラメータと比較します。それらが等しい場合は、Visibility.Visibleを返します。それ以外の場合は、Visibility.Hiddenを返します。

さて、あなたはそれを取ると、このようなXAMLにプラグインすることができます

<Window x:Class="WpfApplication1.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:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:MyConverter x:Key="vConv"/> 
    </Window.Resources> 
    <Grid> 
     <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="25,52,0,0" VerticalAlignment="Top" Width="120"> 
      <ComboBoxItem>Hidden</ComboBoxItem> 
      <ComboBoxItem>Visible</ComboBoxItem> 
     </ComboBox> 

     <Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="219,92,0,0" VerticalAlignment="Top" Visibility="{Binding ElementName=comboBox, Path=SelectedIndex, Converter={StaticResource vConv}, ConverterParameter=1, UpdateSourceTrigger=PropertyChanged}"/> 

    </Grid> 
</Window> 

あなたは私たちがWindow.Resourcesで私たちのMyConverterクラスのインスタンスを作成していることがわかります。バインディングでこれを使用すると、選択されたインデックスに基づいてラベルを表示/非表示できます。今これは非常に基本的なもので、必要な機能をすべて得るためにたくさんの機能を追加することができます。

+0

を行うことができ、そこにあります実際にウィンドウのサイズを変更して展開する方法ですか?たとえば、元々ウィンドウが100×100で、コンボアイテムがクリックされた場合、ウィンドウを100×300に変更できますか? – tralifa

+0

@tralifa WPFのストーリーボードとアニメーションについては、それを読む必要があります。 – niksofteng

1

comboBox Itemの選択時にウィンドウのプロパティMaxHeightMaxWidthを使用することができます。このように:combobox.Useの選択Changeイベントで

この

MainWindow obj= new MainWindow(); 
if(mycombobox.SelectedIndex==0) 
    { 
     obj.MaxWidth="600"; 
     obj.MinWidth="600"; 
    } 
if(mycombobox.SelectedIndex==1) 
    { 
     obj.MaxWidth="200"; 
     obj.MinWidth="200"; 
    } 

か、また、その代わりに、左にある巨大な白い空白のこの

if(mycombobox.SelectedIndex==0) 
     { 
      this.MaxWidth="600"; 
      this.MinWidth="600"; 
     } 
    if(mycombobox.SelectedIndex==1) 
     { 
      this.MaxWidth="200"; 
      this.MinWidth="200"; 
     } 
関連する問題