我正在嘗試找到如何將 int 變數系結為我的樣式值。這是我的簡單 xaml 樣式。
<Style TargetType="Button" x:Key="ButtonMenu">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="White"/>
</Style>
在第四行,我想將值從 14 更改為名為SizeVar bud 的變數,但我不知道該怎么做。我真的嘗試過谷歌搜索,但找不到任何適合我的東西。我知道我應該通過系結以某種方式做到這一點,但不完全是如何做到的。
我嘗試使用不同的系結選項,但找不到適合我的選項。我的英語也不完美,所以也許我誤解了一些東西。
uj5u.com熱心網友回復:
根據您的描述,我解釋了以下內容:
- 您有一個 XAML 檔案,您希望在資源中包含一個名為“ButtonMenu”的樣式。
- 系結到 FontSize 的變數稱為“SizeVar”,包含在設定的 DataContext 中。
有了這個,我測驗了它并且以下作業。
在視圖 (XAML) 中:
...
<Window.DataContext>
<viewmodels:MainViewModel/>
</Window.DataContext>
<Window.Resources>
<Style TargetType="Button" x:Key="ButtonMenu">
<Setter Property="FontSize" Value="{Binding SizeVar}"/>
</Style>
</Window.Resources>
<Grid>
<Button
Content="Test"
Style="{DynamicResource ButtonMenu}"/>
</Grid>
...
在關聯的 ViewModel (C#) 中:
private int _sizeVar;
public int SizeVar
{
get => _sizeVar;
set
{
_sizeVar = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
SizeVar = 14;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461947.html
