クリックした行の値/ IDを取得しようとしています。行が選択されている場合、これは正常に動作します。しかし、ボタンをクリックしようとすると、選択した顧客はnullになります。ここではコマンドパラメータをどうやって行うのですか? 、選択されたお客様は、として来ているOnRunメソッドでWPFリストビュー/データグリッド内のボタン
<Grid>
<ListView ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer}"
Width="Auto">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Address">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
public class VM : ViewModelBase
{
public RelayCommand RunCommand { get; private set; }
private ObservableCollection<Customer> _Customers;
public ObservableCollection<Customer> Customers
{
get { return _Customers; }
set
{
if (value != _Customers)
{
_Customers = value;
RaisePropertyChanged("Customers");
}
}
}
private Customer _SelectedCustomer;
public Customer SelectedCustomer
{
get { return _SelectedCustomer; }
set
{
if (value != _SelectedCustomer)
{
_SelectedCustomer = value;
RaisePropertyChanged("SelectedCustomer");
}
}
}
public VM()
{
Customers = Customer.GetCustomers();
RunCommand = new RelayCommand(OnRun);
}
private void OnRun()
{
Customer s = SelectedCustomer;
}
}
:
ListView and Buttons inside ListView
コードです:
は、私は、これは、次の質問に対する回答を参照してみましたヌル。私はここにデータ列(顧客)が必要です。これはどうすればいいですか?
だから、行が選択されている場合、その後、あなたはそれが動作するボタンをクリックしてください?ボタンがクリックされた行を自動的に選択しようとしましたか? –
それはどういう意味ですか?どのように行を自動的に選択しますか?ユーザーが選択します。私は混乱しています – katie77