2009-09-02 44 views
0

我有一個主要的懸而未決的問題,我現在知道如何進行數據綁定到列表和個人項目,但還有一個問題,我有這個,我需要一個列表框綁定到一些屬性是在一個類。
比如我有綁定在一些XAML中的一類稱爲顯示兩個屬性:如何將ListBox綁定到類中的屬性?

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

我可以在我的XAML中使用這些,但我希望他們能在一個列表框/下拉列表,如何我可以在此列表中顯示我的屬性,並且可以更改它們的值,它是否必須是列表才能列表?
我只是希望能夠從下拉列表中修改這些屬性來切換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

這是一個有趣的解決方案,將屬性從一個Class轉換爲一個列表,我可以用這種方式嘗試,因爲這更像是我想要的解決方案 - 基本上,我手動構建列表並綁定到每個列表項的每個屬性,儘管它起作用,但這個解決方案可能更有用,因爲我有很多屬性可以綁定。 – RoguePlanetoid 2009-09-03 08:39:04

+0

另外,如果您想要添加不同類型的屬性,您可以用容器控件替換CheckBox,並使用按類型綁定的dataTemplate,併爲每種類型添加數據模板(使用複選框編輯布爾值,一個文本框等)。如果你需要這個讓我知道,我會爲你寫一個例子。 – 2009-09-03 13:54:48

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,當你選擇一個選項時,你應該看到but的可見性噸和複選框更改爲反映您的選擇。讓我知道,如果這不是你想要的。

+0

感謝您的,但我知道我沒有正確的措辭我的問題,然而,這已成爲使用屬性更改項目的一個有用示例,我還需要它!我想要一個List類型的複選框來改變我的類中的屬性,我可以讓它們在列表中,但是我需要它們作爲非列表UI中的XAML中的綁定的類屬性。 – RoguePlanetoid 2009-09-02 19:26:06

+0

因此,如果我改變了列表框在我的例子中的CheckBox會是你想要的? – 2009-09-02 20:16:25

0

思考這個解決方案後,發生在我,這是構建在XAML列表,然後使用上的複選框A轉換器的布爾轉換器isChecked在類屬性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> 

然後我可以綁定到這兩個屬性我想(從實際應用這個例子)。

相關問題