如何更改元素 VIA 视图模型的样式,就像我想在单击按钮时切换样式一样。
XAML
<Grid x:Name="LayoutRoot"
DataContext="{Binding Source={StaticResource IndexVMDataSource}}">
<Button Content="Button"
HorizontalAlignment="Left"
Height="42"
Margin="10,49,0,0"
VerticalAlignment="Top"
Width="130"
Command="{Binding OnCommandName, Mode=OneWay}" />
<HyperlinkButton Content="HyperlinkButton"
HorizontalAlignment="Left"
Height="34"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="216"
Style="{Binding QStyle}" />
</Grid>
虚拟机
private string _styleA = "HyperLink-Navi-Container";
private string _styleB = "HyperLink-Navi-Container-2";
private string qStyle;
public string QStyle
{
get
{
return qStyle;
}
set
{
if (qStyle != value)
{
qStyle = value;
NotifyPropertyChanged(Utility.GetPropertyName(() => QStyle));
}
}
}
private ICommand onCommandName = null;
public ICommand OnCommandName
{
get
{
return onCommandName;
}
private set
{
onCommandName = value;
}
}
public void Command()
{
if (QStyle != _styleA)
QStyle = _styleA;
else if (QStyle != _styleB)
QStyle = _styleB;
}