数据绑定似乎不适用于特定部分

时间:2013-02-23 20:28:40

标签: c# wpf data-binding

我正在尝试为学校项目建立一个网上商店。 问题是在底部附近我希望将所选名称放入文本框中,但它似乎无法正常工作

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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}"> ...