2009-09-02 18 views
0

私は主な未解決の問題が1つあります。リストと個々のアイテムにデータバインドする方法はわかっていますが、もう1つ問題があります。リストボックスをクラス内のいくつかのプロパティにバインドする必要があります。例については
私は表示と呼ばれるクラスにいくつかのXAMLにバインドされている2つのプロパティがあります:どのように、私は私のXAMLでこれらを使用することができますが、私は彼らがリストボックス/ドロップダウンリストになりたいListBoxをクラスのプロパティにバインドする方法は?

Public Property ShowEventStart() As Visibility 
Public Property ShowEventEnd() As Visibility 

をこのリストに自分のプロパティが表示され、その値を変更できるようにすることができますか?それはリストである必要がありますか?
ドロップダウンリストのチェックボックスを使用して、ShowEventStartプロパティとShowEventEndプロパティ値の可視性を切り替えるために、これらのプロパティをドロップダウンリストから変更できます。

これはSilverlight 3.0ソリューションでなければなりません。リストではないXAMLでバインドできるものをどのように持つかを理解できず、これらのアイテムを変更するリストとしてバインドされています。

ShowEventStartやShowEventEndなどのクラスプロパティの値を変更するチェックボックスのリストが必要です。他のプロパティがありますが、これが開始になります。

答えて

1

PropertyWrapperクラスを作成し、ウィンドウコードの背後にList<PropertyWrapper>を返し、ListBox.ItemsSourceをバインドするプロパティを公開します。

public class PropertyWrapper 
{ 
    private readonly object target; 
    private readonly PropertyInfo property; 

    public PropertyWrapper(object target, PropertyInfo property) 
    { 
    this.target = target; 
    this.property = property; 
    } 

    public bool Value 
    { 
    get 
    { 
    return (bool) property.GetValue(target, null); 
    } 
    set 
    { 
    property.SetValue(target, value, null); 
    } 
    } 

    public PropertyInfo Property 
    { 
    get 
    { 
    return this.property; 
    } 
    } 
} 

の後ろにあなたの窓コード:

public partial class Window1 : Window, INotifyPropertyChanged 
{ 
public Window1() 
    { 
    InitializeComponent(); 

    properties = new List<PropertyWrapper> 
       { 
        new PropertyWrapper(this, typeof(Window1).GetProperty("A")), 
     new PropertyWrapper(this, typeof(Window1).GetProperty("B")), 
       }; 

    this.DataContext = this; 
    } 

    private List<PropertyWrapper> properties; 
    public List<PropertyWrapper> Properties 
    { 
    get { return properties; } 
    } 


    private bool a; 

    private bool b = true; 

    public bool A 
    { 
    get 
    { 
    return a; 
    } 
    set 
    { 
    if (value != a) 
    { 
    a = value; 
    NotifyPropertyChange("A"); 
    } 
    } 
    } 

    public bool B 
    { 
    get 
    { 
    return b; 
    } 
    set 
    { 
    if (value != b) 
    { 
    b = value; 
    NotifyPropertyChange("B"); 
    } 
    } 
    } 

    protected void NotifyPropertyChange(string propertyName) 
    { 
    if (PropertyChanged != null) 
    { 
    PropertyChanged.Invoke(
    this, new PropertyChangedEventArgs(propertyName)); 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

あなたの窓のマークアップ:

  <ListBox ItemsSource="{Binding Path=Properties}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Text="{Binding Path=Property.Name}"/> 
          <CheckBox IsChecked="{Binding Path=Value}" /> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

希望、これはそのための

+0

これは興味深い解決策です。クラスからプロパティをリストに変換するには、これが私の望むソリューションに似ているので、この方法で試してみてください。リストを手動で作成し、リスト項目ごとに各プロパティにバインドします、私はバインドする多くのプロパティを持っているので、このソリューションはより有用かもしれませんが動作します。 – RoguePlanetoid

+0

また、さまざまなタイプのプロパティを追加する場合は、CheckBoxをコンテナコントロールに置き換え、タイプごとにバインドするdataTemplateを使用し、各タイプのデータテンプレートを追加します(チェックボックスでブール値を編集するには、テキストボックスなど)。あなたがこれを必要とするなら私に知らせてください、そして私はあなたのための例を書くでしょう。 –

0

私は同様のものをモックアップしようとしました。私が質問を誤解した場合、これがあなたのためにどのように機能するかを見て、私に知らせてください。 MainPage.xamlをから

<StackPanel Orientation="Horizontal"> 
     <ListBox SelectedItem="{Binding ShowControls, Mode=TwoWay}" x:Name="VisibilityList"/> 
     <Button Content="Test" Visibility="{Binding ShowControls}"/> 
     <CheckBox Content="Test 2" Visibility="{Binding ShowControls}"/> 
    </StackPanel> 

MainPage.xaml.csの背後にあるコード:

public partial class MainPage : UserControl 
    { 
     VisibilityData visibilityData = new VisibilityData(); 

     public MainPage() 
     { 
      InitializeComponent(); 

      VisibilityList.Items.Add(Visibility.Visible); 
      VisibilityList.Items.Add(Visibility.Collapsed); 

      this.DataContext = visibilityData; 
     } 
    } 

とデータクラス:

public class VisibilityData : INotifyPropertyChanged 
    { 
     private Visibility showControls = Visibility.Visible; 

     public Visibility ShowControls 
     { 
      get { return showControls; } 
      set 
      { 
       showControls = value; 
       OnPropertyChanged("ShowControls"); 
      } 
     } 

     private void OnPropertyChanged(string p) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(p)); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 

あなたがそのコードを実行しますVisibleとCollapsedのオプションを持つListBoxを取得する必要があります。オプションを選択すると、トンとチェックボックスはあなたの選択を反映するように変更されます。それがあなたが探していたものでないかどうか私に教えてください。

+0

感謝を助けますが、私は私が正しく私の質問に言葉で表現していない実現しかし、これは私が必要としたプロパティ変更されたアイテムの使用のための有用な例でした!クラスのプロパティを変更するCheckboxのListを持っていますが、Listでそれらを持つことができますが、リスト以外のUIのXAMLでBindingのClass Propertiesにする必要があります。 – RoguePlanetoid

+0

CheckBoxの私の例でListBoxを変更したら、それはあなたが望むものでしょうか? –

0

これを考えた後、XAMLでListを作成し、チェックボックスでConverterを使用してブールIsCheckedをClassのProperty Visibilityプロパティに変換するという解決策が私に発生しました。私はその後、私が欲しい二つのプロパティにバインドすることができます

  <ComboBox Canvas.Left="6" Canvas.Top="69" Width="274" Height="25" Name="Display"> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding Path=Display.EventStart,Mode=TwoWay,Converter={StaticResource VisibilityConverter}}"/> 
        <TextBlock Text="Event Start"/> 
       </StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding Path=Display.EventEnd,Mode=TwoWay,Converter={StaticResource VisibilityConverter}}"/> 
        <TextBlock Text="Event End"/> 
       </StackPanel> 
      </ComboBox> 

(実際のアプリケーションからこの例):
は、次のようなリストを作成することができます。

関連する問題