【问题标题】:How to pass the selectedItem value of listView to another page in wpf MVVM如何将listView的selectedItem值传递到wpf MVVM中的另一个页面
【发布时间】:2020-04-21 07:26:02
【问题描述】:

我对 wpf 不太熟悉,所以需要帮助。 我想使用绑定将列表视图中所选项目的值传递给另一个页面文本框。 我已成功获得选定的值,但不知道如何在另一页上传递它。 请帮忙

    <ListView x:Name="mobileList" Grid.Column="0" 
              Grid.Row="6" 
              HorizontalAlignment="Center" 
              HorizontalContentAlignment="Left" 
              Height="auto" VerticalAlignment="Bottom"
              ItemsSource="{Binding Path=DataList,Mode=TwoWay}" 
              SelectedItem="{Binding SelectedItem,Mode=TwoWay}">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path=ModelName,Mode=TwoWay}"
                                Header="ModelName" Width="auto"/>
            </GridView>
        </ListView.View>
    </ListView>

    <TextBlock x:Name="txtModelID" Text="{Binding Path=SelectedItem.ModelID, Mode=TwoWay}" Grid.Row="2" 
               Grid.Column="1" Background="Gray" Width="200" HorizontalAlignment="Left" VerticalAlignment="Bottom"
               FontSize="10" Height="20"/>


    <TextBlock x:Name="txtModelName" Text="{Binding Path=SelectedItem.ModelName, Mode=TwoWay}" Grid.Row="3" 
               Grid.Column="1" Background="Gray" Width="200" HorizontalAlignment="Left" VerticalAlignment="Bottom"
               FontSize="10" Height="20"/>


    <TextBlock x:Name="txtBrandID" Text="{Binding Path=SelectedItem.BrandID, Mode=TwoWay}" Grid.Row="4" 
               Grid.Column="1" Background="Gray" Width="200" HorizontalAlignment="Left" VerticalAlignment="Bottom"
               FontSize="10" Height="20"/>

这是选定的项目

private CK_Model _SelectedItem;
    public CK_Model SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            _SelectedItem = value;
            OnPropertyChanged(nameof(SelectedItem));
        }
    }

谢谢。

【问题讨论】:

  • 您要绑定文本框的页面是否嵌入到列表视图的同一个 XAML 中?
  • 不,它没有嵌入在同一页面中,我希望在另一个页面上选择值
  • 这个新页面是否托管在同一个窗口中,因此它的视图模型是否可以由托管原始视图的窗口视图模型实例化?您的窗口视图模型可以实例化此其他页面的视图模型,然后将 SelectedItem 属性(坏名称 - 已经有了含义)传递给它。
  • @Andy 它帮我解决了错误谢谢

标签: wpf listview selecteditem


【解决方案1】:

您可以更改设置器:

    private CK_Model _SelectedItem;
    public CK_Model SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            _SelectedItem = value;
            OnPropertyChanged(nameof(SelectedItem));
            _anotherPageViewModel.YourProperty = _SelectedItem;
        }
    }

【讨论】:

    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多