2009-09-02 63 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> 

然后我可以绑定到这两个属性我想(从实际应用这个例子)。

相关问题