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