2016-05-12 152 views
1

からバインドしましたstackpanelには、子として動的にRadioButtonsが追加されています。 Radiobuttonsの内容は整数です。WPFから動的に作成されたRadioButtonをコード

グリッド(動的にコードから決定されるサイズ)があります。これには、動的にボタンを追加し、ユーザーがボタンの内容を整数値を表す文字列に変更できるようにします。

のStackPanelから任意のラジオボタンをチェックした後、私は彼らの背景色が変化したと同じ番号を持つグリッドからすべてのボタンが欲しい:私は助けを必要とするのはここ

です。

私はWPFを初めて使用しているので、これを達成する方法がわからず、あなたの助けに感謝します。

EDIT: 私は少し進歩を遂げ、私がやっていることは基本的にeverybutton、すべてのラジオボタンのためRadioButton.IsCheckedとRadioButton.ContentでButton.Contentを結合し、私はそれがここでしか最後のラジオボタンのために働くという問題があるさコード(rbuts =ラジオボタンの親コントロール、MyGridボタンの=親コントロール)は、次のとおりです。

for (int z = 0; z < boardSize * boardSize; z++) 
     { 

      Button b1 = MyGrid.Children[z] as Button; 
      for (int i = 0; i < boardSize; i++) 
      { 
       MultiBinding rbtnBinding = new MultiBinding(); 
       rbtnBinding.Converter = new RadioButtonHighlightConverter(); 
       rbtnBinding.Bindings.Add(new Binding("IsChecked") { Source = rbuts.Children[i] }); 
       rbtnBinding.Bindings.Add(new Binding("Content") { Source = rbuts.Children[i] }); 
       rbtnBinding.Bindings.Add(new Binding("Content") { Source = MyGrid.Children[z] }); 
       rbtnBinding.NotifyOnSourceUpdated = true; 
       b1.SetBinding(Button.BackgroundProperty, rbtnBinding); 

      } 

     } 

その私が同じボタンには多くの異なるmultibindingsを設定することはできませんかのように...

+0

答えから[ボタンのスタイルセレクタを使用](http://stackoverflow.com/questions/5082509/use-a -styleselector-for-a-button)はおそらくあなたの問題を解決するでしょう。 –

答えて

0

あなたのボタンのスタイルを置くことができリソース辞書に入れてバインドするEボタンのスタイルやConverterを使用

ButtonStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style x:Key="ButtonStyle1" TargetType="Button"> 
     <Setter Property="Background" Value="Green"/> 
     <Setter Property="FontSize" Value="12"/> 
    </Style> 
    <Style x:Key="ButtonStyle2" TargetType="Button"> 
     <Setter Property="Background" Value="Red"/> 
     <Setter Property="FontSize" Value="14"/> 
    </Style> 
</ResourceDictionary> 

次に、あなたが興味

<Button ... 
     Style="{Binding Path=MyDataProperty, 
         Converter={StaticResource ButtonStyleConverter}}"/> 

そして中のプロパティにスタイルをバインドし、この要件を持っているボタンについてあなたはButtonStylesリソースディクショナリをロードし、値に基づいて目的のスタイルを返します

public class ButtonStyleConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     Uri resourceLocater = new Uri("/YourNameSpace;component/ButtonStyles.xaml", System.UriKind.Relative); 
     ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater); 
     if (value.ToString() == "Some Value") 
     { 
      return resourceDictionary["ButtonStyle1"] as Style; 
     } 
     return resourceDictionary["ButtonStyle2"] as Style; 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

EDIT:変換パラメータの

追加詳細

<RadioButton Content="None" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
    <RadioButton.IsChecked> 
     <Binding Path="MyProperty" 
       Converter="{StaticResource IntToBoolConverter}"> 
      <Binding.ConverterParameter> 
       <sys:Int32>0</sys:Int32> 
      </Binding.ConverterParameter> 
     </Binding> 
    </RadioButton.IsChecked> 
</RadioButton> 
+0

okですが、RadioButtonの場合は、IsCheckedの場合はConverterでチェックする必要がありますが、同じ番号のButtonのバックグラウンドを変更するコンテンツもチェックする必要があるため、boolをIsCheckedを示す値として渡すことはできますが、 RadioButtonのコンテンツをこのboolに沿ってConverterに渡しますか? – HackTheGibson

+0

コンバータパラメータとして整数値をコンバータに渡す方法を示すコードをいくつか追加しました。文字列を渡すこともできます。コードをチェックし、疑問があれば元に戻す。 – ViVi

+0

私は最後のラジオボタンのためだけに機能する理由は分かりませんが、私の部分的な進捗状況で私の質問を編集しました:/ – HackTheGibson

関連する問題