我正在使用Listview将datasource作为observablecollection <empclass>
我的整体课程结构类似于
Class empclass
{
command = new RelayCommand(myfunction, true);
private int _abc;
public int abc
{
get { return _abc;}
set { _abc = value;
onpropertychanged("abc")
}
private int _pqr;
public int pqr
{
get { return _pqr;}
set { _pqr = value;
onpropertychanged("pqr")
}
public void myfunction()
{
messagebox.show((abc+pqr).Tostring());
}
}
我有一个单独的按钮,点击该按钮我想调用所选项目的命令,以显示该对象中存在的受尊重值的abc和pqr的添加。
如果您可以通过小代码示例帮助我,那将会很棒。
由于 Ashpak
答案 0 :(得分:0)
我假设你有一个名为ListView
的{{1}}:
lv
然后,您可以绑定到<ListView Name="lv" ... </ListView>
的{{1}}并使用该项SelectedItem
属性。
ListView
请注意,要使其工作,您必须拥有command
命令,例如:
<Button Command="{Binding ElementName=lv, Path=SelectedItem.command}">Button Text</Button>
答案 1 :(得分:0)
试试这个: 1. XAML代码:
<Window x:Class="SoButtonBindingHelpAttempt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:soButtonBindingHelpAttempt="clr-namespace:SoButtonBindingHelpAttempt"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<soButtonBindingHelpAttempt:MainViewModel/>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding ObservableCollection}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type soButtonBindingHelpAttempt:Empclass}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition Width="120"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding abc, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding pqr, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBlock>
<Button Grid.Column="2" Command="{Binding Command}" Content="Press me!"></Button>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid></Window>
2。 ViewModel代码型号代码:
public class MainViewModel:BaseObservableObject
{
public MainViewModel()
{
ObservableCollection = new ObservableCollection<Empclass>(new List<Empclass>
{
new Empclass{abc=2, pqr = 3},
new Empclass{abc=5, pqr = 7},
new Empclass{abc=11, pqr = 13},
new Empclass{abc=17, pqr = 19}
});
}
public ObservableCollection<Empclass> ObservableCollection { get; set; }
}
public class Empclass : BaseObservableObject
{
private ICommand _command;
private int _abc;
private int _pqr;
public ICommand Command
{
get { return _command ?? (_command = new RelayCommand(myfunction)); }
}
public int abc
{
get { return _abc; }
set
{
_abc = value;
OnPropertyChanged("abc");
}
}
public int pqr
{
get { return _pqr; }
set
{
_pqr = value;
OnPropertyChanged("pqr");
}
}
private void myfunction()
{
//add you command logic here
var temp = pqr;
pqr = abc;
abc = temp;
}
}
3。 MVVM部件实现:
public class BaseObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
{
var propName = ((MemberExpression)raiser.Body).Member.Name;
OnPropertyChanged(propName);
}
protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
OnPropertyChanged(name);
return true;
}
return false;
}
}
public class RelayCommand<T> : ICommand
{
readonly Action<T> _execute;
readonly Func<T, bool> _canExecute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}
public void RefreshCommand()
{
var cec = CanExecuteChanged;
if (cec != null)
cec(this, EventArgs.Empty);
}
public bool CanExecute(object parameter)
{
if (_canExecute == null) return true;
return _canExecute((T)parameter);
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
}
public class RelayCommand : RelayCommand<object>
{
public RelayCommand(Action execute, Func<bool> canExecute = null)
: base(_ => execute(),
_ => canExecute == null || canExecute())
{
}
}
此致