如何在不同的控件

时间:2016-03-13 07:44:46

标签: c# wpf mvvm data-binding listbox

在我的应用程序中,我有一个包含ListBox的窗口,以及应显示其当前所选项目的不同属性的控件。这些控制措施是:

  1. TextBox应该显示'姓名'属性。
  2. TextBox应该显示' DataFile`属性。
  3. DataGrid应显示' TItems属性,即ObservableCollection
  4. 我尝试将SelectedItem绑定到一个对象,然后将该对象的不同属性绑定到上面提到的控件,但没有成功。

    窗口:

    enter image description here

    我的观点:

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ReportMaker"
        xmlns:ViewModel="clr-namespace:ReportMaker.ViewModel" x:Class="ReportMaker.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <ViewModel:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <Button x:Name="button" Content="Create" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75"/>
        <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="120"/>
        <ListBox x:Name="listBox" HorizontalAlignment="Left" Margin="10,10,0,36.667" Width="119" ItemsSource="{Binding ReportItems}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    
        <StackPanel HorizontalAlignment="Left" Height="274" Margin="134,10,0,0" VerticalAlignment="Top" Width="375" DataContext="{Binding SelectedReportItem}">
            <StackPanel.Resources>
                <Style x:Key="ControlBaseStyle" TargetType="{x:Type Control}">
                    <Setter Property="Margin" Value="0, 10, 0, 0" />
                </Style>
            </StackPanel.Resources>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name:"/>
                <TextBox Width="150" Text="{Binding Name}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Data File:"/>
                <TextBox Width="150" Text="{Binding ID}"/>
            </StackPanel>
            <DataGrid Height="190" VerticalAlignment="Bottom" ItemsSource="{Binding TItems}"/>
        </StackPanel>
        <Button x:Name="button_Copy" Content="Save" HorizontalAlignment="Right" Margin="0,0,92,10" VerticalAlignment="Bottom" Width="75"/>
    
    </Grid>
    </Window>
    

    我的ViewModel:

    public class MainViewModel
    {
        public ObservableCollection<ReportItem> ReportItems { get; set; }
        public object SelectedReportItem { get; set; }
        public MainViewModel()
        {
            ReportItems = new ObservableCollection<ReportItem>();
            ReportItems.Add(Example);
        }
        public ReportItem Example = new TextReportItem() { Name = "John", DataFile = "try.txt"};
    }
    

    ReportItem:

    public class ReportItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string DataFile { get; set; }
    }
    

    TextReportItem:

    public class TextReportItem : ReportItem
    {
        public ObservableCollection<TextParcel> TItems { get; set; }
    }
    public class TextParcel
    {
        char Delimiter { get; set; }
        string LineExp { get; set; }
        string Result { get; set; }
        string IgnoreLine { get; set; }
        int DesiredResultIndexInLine { get; set; }
    }
    

    编辑:因为我使用MVVM,我更喜欢在视图中只使用XAML,没有代码。

    编辑2:

    感谢S.Akbari,我成功地在TextBox控件中查看了所需的属性,其代码如下:

    <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name:"/>
                <TextBox Width="150" Text="{Binding ElementName=listBox, Path=SelectedItem.Name}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Data File:"/>
                <TextBox Width="150" Text="{Binding ElementName=listBox, Path=SelectedItem.DataFile}"/>
            </StackPanel>
    

    但是当我的DataGrid应用相同的逻辑时,由于某种原因它失败了:

    <DataGrid Height="190" VerticalAlignment="Bottom" ItemsSource="{Binding ElementName=listBox, Path=SelectedItem.TItmes}" />
    

    我也尝试过:

    <DataGrid Height="190" VerticalAlignment="Bottom" DataContext="{Binding ElementName=listBox, Path=SelectedItem}" ItemsSource="{Binding TItems}"/>
    

    还有:

    <DataGrid Height="190" VerticalAlignment="Bottom" DataContext="{Binding ElementName=listBox, Path=SelectedItem}">
                <DataTemplate>
                    <TextBlock Text="{Binding TItems}" />
                </DataTemplate>
            </DataGrid>
    

1 个答案:

答案 0 :(得分:1)

如果您使用MVVM,您的视图模型应该引发属性更改事件
您应该实现INotifyPropertyChanged 并将所选项目更改为完整属性 见:How to: Implement Property Change Notification