简体   繁体   English

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

[英]Databinding doesn't seem to work on a specific part

I was trying to make a webstore for a school project. 我试图为学校项目建立一个网上商店。 The problem is that near the bottom i want to get the selected name into the textbox, but it just doesn't seem to work 问题是在底部附近,我想将选定的名称输入文本框,但似乎不起作用

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

This part just doesnt seem to work... 这部分似乎不起作用...

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

Any help would be appreciated. 任何帮助,将不胜感激。

This is example using MVVM . 这是使用MVVM的示例。 Take a look at it. 看一看。 Once you understand how it works, you'll ask yourself how did you do anything at all without it :D 一旦了解了它的工作原理,您就会问自己,如果没有它,您是怎么做的:D

This is how you should modify your window (XAML) class 这是修改窗口(XAML)类的方式

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

Now you should create an ViewModel for that window, we'll name it BrowseStoreViewModel 现在您应该为该窗口创建一个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)));
}

Now just set ViewModel.SelectedItem to currently selected item (on selection change) and TextBox should update automatically. 现在,只需将ViewModel.SelectedItem设置为当前选定的项目(在选择更改时),TextBox应该会自动更新。 I don't exactly know how to direcly bind selected row to ViewModel.SelectedItem but it should look something like this: 我不完全知道如何将选定的行直接绑定到ViewModel.SelectedItem,但是它看起来应该像这样:

<GridView AllowsColumnReorder="False" SelectedValue="{Binding SelectedItem}"> ...

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

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