2013-09-27 73 views
0

我有一个包含一些“应用程序”对象的列表框。 “应用程序”对象可以启动或停止。绑定不更新命令

对于我的Listbox中的每个元素,我有2个按钮,第一个启动应用程序,第二个停止应用程序。

但是,当我点击开始按钮时,它并没有正确更新绑定“IsRunning”。 尽管“CommandManager.InvalidateRequerySuggested();”在单击应用程序内部时,CanExecute of命令被重新评估

因此,我的启动按钮保持启用状态,我的停止按钮从不显示。

<ListBox Grid.Row="1" ItemsSource="{Binding Applications}" Grid.ColumnSpan="3" BorderThickness="0" Background="#FFE8E8E8" HorizontalContentAlignment="Stretch"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <Button Margin="5,0" Content = "Start" 
       Command="{Binding StartCommand}" 
       Visibility="{Binding IsRunning, Converter={Converters:InvertedBoolToVisibilityConverter}}"/> 
     <Button Margin="5,0" Content = "Stop" 
       Command="{Binding StopCommand}" 
       Visibility="{Binding IsRunning, Converter={Converters:BoolToVisibilityConverter}}"/> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

在ApplicationViewModel:

public bool IsRunning 
{ 
    get 
    { 
    return this.m_IsRunning; 
    } 
    set 
    { 
    this.m_IsRunning = value; 
    this.OnPropertyChanged("IsRunning"); 
    CommandManager.InvalidateRequerySuggested(); 
    } 
} 
+1

愚蠢的问题:你是否更新'IsRunning'在命令回调?你是否更新'm_IsRunning'? – meilke

+0

是的,IsRunning已更新,因为在另一个视图中,每件事情都正确更新... –

+0

OMG!对不起,这是正确的......在另一个线程中,我设置了m_IsRunning而不是IsRunning ... –

回答

0

我从来没有见过这样的引用的转换器。您需要somwhere创建它们,然后用StaticResource/DynamicResource引用它们:

<Window.Resources> 
    <Converters:InvertedBoolToVisibilityConverter x:Key="invBoolConv"/> 
</Window.Resources> 

<Button Margin="5,0" Content = "Start" 
     Command="{Binding StartCommand}" 
     Visibility="{Binding IsRunning, Converter={StaticResource invBoolConv}"/> 
+1

如果您在其上实现“MarkupExtension”,则可以使用这样的转换器... –