绑定 ListView SelectedItem 列 MVVM
Bind ListView SelectedItem Column MVVM
我有以下XAML
<ListView ItemsSource="{Binding Franchises, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedFranchise, Mode=TwoWay}" SelectionMode="Single" Height="150">
<ListView.View>
<GridView>
<GridViewColumn x:Name="FranchiseID" Header="ID" DisplayMemberBinding="{Binding FranchiseID}"/>
<GridViewColumn Header="Franchise Name" DisplayMemberBinding="{Binding FranchiseName}"/>
</GridView>
</ListView.View>
</ListView>
我想将 FranchiseID
传递给我的 Presenter
视图模型 SelectedFranchise
值。
Presenter
视图模型
public class Presenter : ObservableObject
{
private DataView _Franchises = Controller.BaseballDB.Tables["Franchise"].DefaultView;
private long _SelectedFranchise;
public DataView Franchises
{
get
{
Controller.Refresh();
_Franchises = Controller.BaseballDB.Tables["Franchise"].DefaultView;
return _Franchises;
}
set
{
Controller.Refresh();
_Franchises = Controller.BaseballDB.Tables["Franchise"].DefaultView;
RaisePropertyChangeEvent("Franchises");
}
}
public long SelectedFranchise
{
get { return _SelectedFranchise; }
set
{
_SelectedFranchise = value;
Debug.Print(_SelectedFranchise.ToString());
}
}
}
现在它给我一个错误,因为它想将 Selected Item 的整个字符串传递给 属性。我在网上搜索过,但显然每个人都希望只有 select 一个只有一列的项目。
将 SelectedValuePath
与 SelectedValue
结合使用。请确保 FranchiseID
和 SelectedFranchise
.
的数据类型相同
<ListView ItemsSource="{Binding Franchises, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="FranchiseID" SelectedValue="{Binding SelectedFranchise}" SelectionMode="Single" Height="150">
<ListView.View>
<GridView>
<GridViewColumn x:Name="FranchiseID" Header="ID" DisplayMemberBinding="{Binding FranchiseID}"/>
<GridViewColumn Header="Franchise Name" DisplayMemberBinding="{Binding FranchiseName}"/>
</GridView>
</ListView.View>
</ListView>
我有以下XAML
<ListView ItemsSource="{Binding Franchises, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedFranchise, Mode=TwoWay}" SelectionMode="Single" Height="150">
<ListView.View>
<GridView>
<GridViewColumn x:Name="FranchiseID" Header="ID" DisplayMemberBinding="{Binding FranchiseID}"/>
<GridViewColumn Header="Franchise Name" DisplayMemberBinding="{Binding FranchiseName}"/>
</GridView>
</ListView.View>
</ListView>
我想将 FranchiseID
传递给我的 Presenter
视图模型 SelectedFranchise
值。
Presenter
视图模型
public class Presenter : ObservableObject
{
private DataView _Franchises = Controller.BaseballDB.Tables["Franchise"].DefaultView;
private long _SelectedFranchise;
public DataView Franchises
{
get
{
Controller.Refresh();
_Franchises = Controller.BaseballDB.Tables["Franchise"].DefaultView;
return _Franchises;
}
set
{
Controller.Refresh();
_Franchises = Controller.BaseballDB.Tables["Franchise"].DefaultView;
RaisePropertyChangeEvent("Franchises");
}
}
public long SelectedFranchise
{
get { return _SelectedFranchise; }
set
{
_SelectedFranchise = value;
Debug.Print(_SelectedFranchise.ToString());
}
}
}
现在它给我一个错误,因为它想将 Selected Item 的整个字符串传递给 属性。我在网上搜索过,但显然每个人都希望只有 select 一个只有一列的项目。
将 SelectedValuePath
与 SelectedValue
结合使用。请确保 FranchiseID
和 SelectedFranchise
.
<ListView ItemsSource="{Binding Franchises, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="FranchiseID" SelectedValue="{Binding SelectedFranchise}" SelectionMode="Single" Height="150">
<ListView.View>
<GridView>
<GridViewColumn x:Name="FranchiseID" Header="ID" DisplayMemberBinding="{Binding FranchiseID}"/>
<GridViewColumn Header="Franchise Name" DisplayMemberBinding="{Binding FranchiseName}"/>
</GridView>
</ListView.View>
</ListView>