1

我试图为一个学校项目创建一个网上商店。问题是在底部附近我想将所选名称放入文本框中,但它似乎不起作用

     <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Default="clr-namespace" x:Class="Project.BrowseStore"
    Title="Browse the store" Height="501.786" Width="735.714" WindowStartupLocation="CenterScreen">
<Grid>
    <Label Content="" HorizontalAlignment="Left" Margin="107.94,65.044,0,0" VerticalAlignment="Top"/>
    <ComboBox x:Name="CmbSort" HorizontalAlignment="Left" Margin="270.627,65.044,0,0" VerticalAlignment="Top" Width="120" IsReadOnly="True" ToolTip="Sorteren" Tag="Sorteren" Text="Sorteren">
        <ComboBoxItem Content="Alle" Selected="ComboBoxItem_Selected_Alle"/>
        <ComboBoxItem Content="Prijs groter dan" Selected="ComboBoxItem_Selected_Prijs"/>
        <ComboBoxItem Content="Prijs kleiner dan" Selected="ComboBoxItem_Selected_Prijs"/>
        <ComboBoxItem Content="Naam" Selected="ComboBoxItem_Selected_Naam"/>
        <ComboBoxItem Content="Boeken" Selected="ComboBoxItem_Selected_Alle"/>
        <ComboBoxItem Content="Games" Selected="ComboBoxItem_Selected_Alle"/>
        <ComboBoxItem Content="Jaaruitgave" Selected="ComboBoxItem_Selected_Jaaruitgave"/>
    </ComboBox>
    <Label Content="Zoeken op" HorizontalAlignment="Left" Margin="122.94,65.044,0,0" VerticalAlignment="Top" Width="82.77" Height="25.96"/>
    <TextBox x:Name="TxtSort" IsEnabled="False" HorizontalAlignment="Left" Height="23" Margin="427.344,65.044,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="-0.062,0.519"/>
    <Button x:Name="BtnGo" Content="Go" HorizontalAlignment="Left" Margin="581.075,67.044,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    <TextBox x:Name="txtTest" HorizontalAlignment="Left" Height="23" Margin="143.761,117.284,0,0" TextWrapping="Wrap" Text="&#xA;" VerticalAlignment="Top" Width="120"/>
    <Button Content="Order" HorizontalAlignment="Left" Margin="130.71,442.226,0,0" VerticalAlignment="Top" Width="75"/>
    <DockPanel x:Name="Panel" Height="282.226" LastChildFill="False" Margin="23.493,155,0,0" VerticalAlignment="Top" Width="683.881">
        <ListView x:Name="lstSort" HorizontalAlignment="Left" Height="138.209" VerticalAlignment="Top" Width="683.881" DockPanel.Dock="Top" IsSynchronizedWithCurrentItem="True">
            <ListView.View>
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}"/>
                    <GridViewColumn Header="Naam"  DisplayMemberBinding="{Binding Naam}"/>
                    <GridViewColumn Header="Prijs" DisplayMemberBinding="{Binding Prijs}"/>
                    <GridViewColumn Header="Jaaruitgave" DisplayMemberBinding="{Binding Jaaruitgave}"/>
                    <GridViewColumn Header="Afbeelding" DisplayMemberBinding="{Binding Afbeelding}"/>
                    <GridViewColumn Header="Uitgever" DisplayMemberBinding="{Binding Uitgever}"/>
                    <GridViewColumn Header="Stock" DisplayMemberBinding="{Binding Stock}"/>



                </GridView>

            </ListView.View>

        </ListView>
        <TextBox Height="144.017" TextWrapping="Wrap" Text="{Binding Naam}" VerticalAlignment="Top" Width="120"/>
    </DockPanel>

</Grid>

这部分似乎不起作用......

     <TextBox Height="144.017" TextWrapping="Wrap" Text="{Binding Naam}" VerticalAlignment="Top" Width="120"/>

任何帮助,将不胜感激。

4

1 回答 1

1

这是使用MVVM的示例。看看它。一旦你了解它是如何工作的,你会问自己没有它你是怎么做任何事情的:D

这就是你应该如何修改你的窗口(XAML)类

<Window ... DataContext="ViewModel">
    <Window.Resources>
        <local:BrowseStoreViewModel x:Key="ViewModel" />
    </Window.Resources>
    ...
    <DockPanel ...>
        ...
        <TextBox Text="{Binding SelectedItem.Naam}" ... />
    </DockPanel>

现在您应该为该窗口创建一个 ViewModel,我们将其命名BrowseStoreViewModel

public class BrowseStoreViewModel : DependencyObject {
    // Dependency property for Selected Item
    // Replace "ItemType" with type you're populating GridView
    public ItemType SelectedItem {
        get { return (ItemType )GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }
    public static readonly DependencyProperty SelectedItemProperty=
        DependencyProperty.Register("SelectedItem", typeof(ItemType), typeof(BrowseStoreViewModel), new PropertyMetadata(default(ItemType)));
}

现在只需将 ViewModel.SelectedItem 设置为当前选定的项目(在选择更改时),TextBox 应该会自动更新。我不完全知道如何将选定的行直接绑定到 ViewModel.SelectedItem 但它应该看起来像这样:

<GridView AllowsColumnReorder="False" SelectedValue="{Binding SelectedItem}"> ...
于 2013-02-23T21:19:45.730 回答