我在这里有一些学术问题。看看标记:
<Grid Margin="10,10,10,10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ComboBox SelectedIndex="{Binding SelectedIndex}"
Margin="5"
Width="100">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding TestCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ComboBox},
Path=SelectedIndex}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ComboBoxItem>Item1</ComboBoxItem>
<ComboBoxItem>Item2</ComboBoxItem>
<ComboBoxItem>Item3</ComboBoxItem>
</ComboBox>
<Button Grid.Row="1"
Content="Set SelectedIndex to 0"
Width="100"
Command="{Binding ButtonCommand}"
Margin="5">
</Button>
</Grid>
这是DataContext
类。
class Class1Context : ViewModelBase
{
private int _selectedIndex;
public Int32 SelectedIndex
{
get { return _selectedIndex; }
set
{
_selectedIndex = value;
RaisePropertyChanged("SelectedIndex");
}
}
private RelayCommand<Object> _testCommand;
public RelayCommand<Object> TestCommand
{
get
{
return _testCommand ?? (_testCommand =
new RelayCommand<Object>(TestMethod));
}
}
private void TestMethod(Object obj)
{
var index = (Int32) obj;
var selIndex = SelectedIndex;
}
private RelayCommand _buttonCommand;
public RelayCommand ButtonCommand
{
get
{
return _buttonCommand ?? (_buttonCommand =
new RelayCommand(ButtonCommandMethod));
}
}
private void ButtonCommandMethod()
{
SelectedIndex = 0;
}
}
那么,问题出在哪里?这里是。当我选择Item2
或Item3
时,SelectedIndex
属性等于1或2.例如,我现在点击Item2
和SelectedIndex
等于1。因此,当我点击Button
并将SelectedIndex
设置为0时,它会在SelectionChanged
中生成事件Combobox
。这是逻辑上的。然后,event触发有界命令TestCommand
。
在TestMethod
索引(CommandParameter
)中等于1( 1!)并且这是一个问题,尽管SelectedIndex
DataContext
等于为0(零)。
那么,它是Wpf
错误还是其他什么?
答案 0 :(得分:0)
我是这样做的:
rootProject.name = 'modules-business'
include 'business-core'
.. snip business-* modules ..
// includeBuild '../modules-technical/'
这种方式对我来说很好。我知道每个ComboBoxItem使用相同的代码会产生一些代码开销。
如果您的ComboBox需要动态加载,那么您应该能够在代码中添加Interaction.Triggers
答案 1 :(得分:-1)
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding TestCommand}" ></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>