1

我想将 Groupboxes 绑定到我的堆栈面板。

C#:

internal ObservableCollection<GroupBox> BindingTest
{
    get
    {
        ObservableCollection<GroupBox> collection = new ObservableCollection<GroupBox>();
        for (int counter = 0; counter < 5; counter++)
        {
            GroupBox groupBox = new GroupBox();
            groupBox.Header = " ... Header ... ";
            groupBox.Content = " ... Content ... ";
            collection.Add(groupBox);
        }

            return collection;
    }
}

和 xaml 代码:

<StackPanel x:Name="Dynamic" Grid.Row="1" Margin="29,118,6,0">
    <ItemsControl ItemsSource="{Binding Path=BindingTest}" Height="482">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <GroupBox>
                    <GroupBox.Header>{Binding Path=Header}</GroupBox.Header>
                    <GroupBox.Content>{Binding Path=Content}</GroupBox.Content>
                </GroupBox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

我的推理错误在哪里?

4

2 回答 2

1

在 WPF Binding 中,您通常不绑定到控件,您只需绑定到对象并让标记处理 UI 细节。理想情况下,您的对象应该实现INotifyPropertyChanged以通知 Binding 属性更改。控件创建已经由 处理DataTemplate,它已经GroupBox为您创建了一个并将其属性绑定到绑定中使用的对象的属性。

ViewModel如果可能,您应该对 UI一无所知,或使用任何控件。MVVM 模式旨在解耦 UI 和数据/逻辑。

您的数据对象的类可能如下所示:

public class DataObject : INotifyPropertyChanged
{
    private string header;
    private string content;

    public string Header {
        get { return header; }
        set { header = value; RaisePropertyChanged("Header"); }
    }

    public string Content {
        get { return content; }
        set { content= value; RaisePropertyChanged("Content"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        var handler = PropertyChanged;
        if (handler != null) { 
            handler(this, new PropertyChangedEventArgs(propertyName)); 
        }
    }
}

您绑定到的属性也看起来不同:

internal ObservableCollection<DataObject> BindingTest
{
    get
    {
        ObservableCollection<DataObject> collection = new ObservableCollection<DataObject>();
        for (int counter = 0; counter < 5; counter++)
        {
            DataObject item = new DataObject ();
            item.Header = " ... Header ... ";
            item.Content = " ... Content ... ";
            collection.Add(item );
        }

            return collection;
    }
}

我假设每次访问属性时创建一个新集合只是为了测试目的。

于 2012-06-22T07:19:27.717 回答
0

只需将BindingTest属性更改为public字段......它会自动将数据绑定到用户界面......

基本上 xaml 允许您将公共属性绑定到 UI,因为我们对内部和私有等其他字段有限制。

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }


        public ObservableCollection<GroupBox> BindingTest
        {
            get
            {
                ObservableCollection<GroupBox> collection = new ObservableCollection<GroupBox>();
                for (int counter = 0; counter < 5; counter++)
                {
                    GroupBox groupBox = new GroupBox();
                    groupBox.Header = " ... Header ... ";
                    groupBox.Content = " ... Content ... ";
                    collection.Add(groupBox);
                }

                return collection;
            }
        }
    }

xml代码如下

<Grid >
        <StackPanel x:Name="Dynamic" >
            <ItemsControl ItemsSource="{Binding BindingTest, RelativeSource={RelativeSource AncestorType={x:Type this:MainWindow}}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <GroupBox Header="{Binding Path=Header}"
                                  Content="{Binding Path=Content}">
                        </GroupBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </Grid>
于 2012-06-22T07:28:30.850 回答