当我修改状态时,我想要一个按钮来更新其内容的图像源。
XAML:
<ItemsControl ItemsSource="{Binding PlayerStartups}" Grid.Column="0" Grid.Row="1" Grid.RowSpan="3" Grid.ColumnSpan="2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding PlayerStartups.Count}" Margin="10"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ComboBox ItemsSource="{Binding Source={StaticResource PlayerStartupStatusProvider}}"
SelectedItem="{Binding Status}"
x:Name="DataTemplateComboBox"
Margin="5" Grid.Column="0">
<ComboBox.ItemTemplate ... />
</ComboBox>
<Button Margin="5" Grid.Column="1"
Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"
Command="{Binding DataContext.SetPlayerPropertiesCommand,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
CommandParameter="{Binding}">
<Image Source="{Binding TokenSource}"
Stretch="Fill"/>
</Button>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
PlayerStartups
是ObservableCollection
。每当组合框被修改为特定值时,按钮内容图像的来源应该改变,但这不会发生。
我怎么能做到这一点?
这是负责价值变化的事件处理程序的一部分:
....
if (status is ok)
{
((PlayerStartup)s).TokenSource = "pack://siteoforigin:,,,/Images/pic.jpg";
}
.....
我想在视图代码后面没有任何代码的情况下这样做。
PlayerStartup
的属性和字段:
private StartMenuViewModel.Status _status;
private string _character;
private string _name;
private string _tokenSource;
public StartMenuViewModel.Status Status { get { return _status; } set { _status = value; OnStatusChanged(); } }
public string Character { get { return _character; } set { _character = value; OnCharacterChanged(); } }
public string TokenSource { get { return _tokenSource; } set { _tokenSource = value; } }