我是XAML和WPF的新手。 我的目的是通过ComboBox更改出现在窗口中的所有“文本”控件的所有FontSize。
我在窗口中声明了自己的风格:
<Style x:Name="ControlBaseStyle" x:Key="ControlBaseStyle" TargetType ="{x:Type Control}">
<Setter Property="FontSize" Value="11"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
.
.
.
</Window.Resources>
我想将第一个设置器链接到我的ComboBox:
<ComboBox Name="CBFontSize" Style="{StaticResource ControlBaseStyle}" SelectedIndex="3" HorizontalAlignment="Stretch" VerticalAlignment="Center" SelectionChanged="CBFontSize_SelectionChanged">
<ComboBoxItem>8</ComboBoxItem>
<ComboBoxItem>9</ComboBoxItem>
<ComboBoxItem>10</ComboBoxItem>
<ComboBoxItem>11</ComboBoxItem>
<ComboBoxItem>12</ComboBoxItem>
<ComboBoxItem>14</ComboBoxItem>
<ComboBoxItem>16</ComboBoxItem>
<ComboBoxItem>18</ComboBoxItem>
<ComboBoxItem>20</ComboBoxItem>
<ComboBoxItem>22</ComboBoxItem>
<ComboBoxItem>24</ComboBoxItem>
</ComboBox>
我尝试通过Binding设置FontSize-Setter的值,但是它不起作用:
<Setter Property="FontSize" Value="{Binding SelectedItem,ElementName=CBFontSize}"/>
我尝试通过Dynamicresource设置它(并使用CBFontSize_SelectionChanged修改值),但没有任何反应:
<Style x:Name="ControlBaseStyle" x:Key="ControlBaseStyle" TargetType ="{x:Type Control}">
<Setter Property="FontSize" Value="{Dynamicresource ResourceKey=MyFontSize}"/>
</Style>
private void CBFontSize_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
if (double.TryParse(CBFontSize.SelectedItem?.ToString() ?? "",out double mfs))
{
Resources["MyFontSize"] = mfs;
}
}
我尝试通过DataTrigger进行设置,但我不知道如何传递值:
<Style.Triggers>
<DataTrigger x:Name="MyDataTrigger" Binding="{Binding SelectedItem,ElementName=CBFontSize}">
<Setter Property="FontSize" Value="{Binding Value,ElementName=MyDataTrigger}"/>
</DataTrigger>
</Style.Triggers>
有人可以帮我吗?
PS,我不假装解决方案必须遵循我的代码/方法,任何方法对我来说都很好。 我只是想象有一个相对简单的解决方案,我太愚蠢/无知了