繁体   English   中英

如何在不同控件中显示列表框所选项目的属性

[英]How to show properties of listbox selected item in different controls

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

  1. 应显示“名称”属性的TextBox
  2. 应显示“DataFile”属性的TextBox
  3. DataGrid应该显示 'TItems 属性,它是一个ObservableCollection

我尝试将SelectedItem绑定到一个对象,然后将该对象的不同属性绑定到上面提到的控件,但没有成功。

窗户:

在此处输入图片说明

我的看法:

<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>

我的视图模型:

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"};
}

报告项目:

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

文本报告项:

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>

如果您使用 MVVM,您的视图模型应该引发属性更改事件
您应该实现 INotifyPropertyChanged 并将所选项目更改为完整属性,请参阅:如何:实现属性更改通知

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM