gpt4 book ai didi

silverlight - 绑定(bind)样式名称并更改样式 VIA View Model

转载 作者:行者123 更新时间:2023-12-03 10:38:00 24 4
gpt4 key购买 nike

如何更改元素 VIA View 模型的样式,就像我想在单击按钮时切换样式一样。

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;
}

最佳答案

您的 QStyle属性必须是 Style 类型:

private Style qStyle;
public Style QStyle
{
get { return qStyle; }
set
{
if (qStyle != value)
{
qStyle = value;
NotifyPropertyChanged(Utility.GetPropertyName(() => QStyle));
}
}
}

或者您使用 binding converter在您的样式绑定(bind)中,它返回适当的 Style对于给定的字符串(例如 Style 资源的键):
<HyperlinkButton ...
Style="{Binding QStyle, Converter={StaticResource YourStringToStyleConverter}}" />

由于您没有显示您定义样式的位置,我猜它们在您的 UserControl 的 Resources 中。 .然后,您可以通过以下方式获取它们:
Style style = Resources["HyperLink-Navi-Container"] as Style;

如果资源在 App.xaml 中定义,您可以编写
Style style = Application.Current.Resources["HyperLink-Navi-Container"] as Style;

关于silverlight - 绑定(bind)样式名称并更改样式 VIA View Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com