如何使用WPF和MVVM模式将控件绑定到列表框中的选定项?

时间:2013-06-22 18:41:46

标签: c# wpf mvvm

我目前有一种工作方法,但不能满足我的需要。以下是“设置”窗口的外观:

现在我将Department选项卡DataContext属性绑定到Department对象的ObservableCollection,然后我只是在右侧的文本框中引用Department对象的属性。以下是此选项卡的代码:

<TabItem Header="Department Settings"
             DataContext="{Binding Departments}">
        <DockPanel Margin="3,3,3,3">
            <DockPanel DockPanel.Dock="Left"
                       Width="200">
                <StackPanel DockPanel.Dock="Bottom"
                            Margin="0,5,0,0"
                            Orientation="Horizontal">
                    <TextBox x:Name="tbxAddDepartmentName"
                             Width="160"
                             Padding="0,5,0,5" />
                    <Button x:Name="btnAddDepartment"
                            Content="Add"
                            Margin="5,0,5,0"
                            Padding="5,5,5,5"
                            Command="{Binding AddDepartmentCommand, UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
                <ListBox ItemsSource="{Binding}" />
            </DockPanel>

            <StackPanel DockPanel.Dock="Bottom">
                <Button Content="Save Changes"
                        Padding="5,5,5,5"
                        HorizontalAlignment="Right" />
            </StackPanel>
            <Grid Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <!-- Labels -->
                <Label Grid.Row="0" Grid.Column="0">Department Name:</Label>
                <Label Grid.Row="1" Grid.Column="0">Report Number:</Label>
                <Label Grid.Row="2" Grid.Column="0">Address:</Label>

                <!-- Input -->
                <TextBox x:Name="tbxDepartmentName"
                         Text="{Binding Name}"
                         Grid.Row="0" Grid.Column="1"
                         VerticalContentAlignment="Center"
                         Margin="0,5,0,5"
                         Padding="5" />
                <TextBox Grid.Row="1" Grid.Column="1"
                         Text="{Binding ReportNumber}"
                         VerticalContentAlignment="Center"
                         Margin="0,5,0,5"
                         Padding="5" />
                <TextBox Grid.Row="2" Grid.Column="1"
                         Text="{Binding Address}"
                         VerticalContentAlignment="Center"
                         AcceptsReturn="True"
                         Margin="0,5,0,5"
                         Padding="5" />
            </Grid>
        </DockPanel>
    </TabItem>

我想要做的是有一个指向特定Department对象的指针,该对象是使用ViewModel中的ListBox选择的。这样,我应该可以对右边的文本框进行单向绑定,并在点击“保存更改”按钮后将更改保存到SQL Compact DB。

1 个答案:

答案 0 :(得分:1)

示例ViewModel:

public class ViewModel : INotifyPropertyChanged, INotifyPropertyChanging
{
    public class MyObj
    {
        public string Test1 { get; set; }
        public string Test2 { get; set; }
    }

    #region selectedItem

    private MyObj _selectedItem;

    public MyObj selectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
            if (_selectedItem != value)
            {
                NotifyPropertyChanging("selectedItem");
                _selectedItem = value;
                NotifyPropertyChanged("selectedItem");
            }
        }
    }
    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify the page that a data context property changed
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    #region INotifyPropertyChanging Members

    public event PropertyChangingEventHandler PropertyChanging;

    // Used to notify the data context that a data context property is about to change
    protected void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    #endregion
}

添加到ListBox:

SelectedItem="{Binding selectedItem, UpdateSourceTrigger=PropertyChanged}"

您的dataContext应该是整个ViewModel,而不是集合。 然后你可以在listBox中设置

ItemsSource="{Binding Departments}"

并在网格中

DataContext="{Binding Departments}"