WPF:通过 ComboBox 选择上的 DataTrigger 更改样式 FontSize

WPF: Change Style FontSize via DataTrigger on ComboBox selection

我是 XAML 和 WPF 的新手。 我的目的是通过 ComboBox 更改出现在我的 window 中的所有 "text" 控件的所有 FontSize。

ComboBoxFontSize

我在 window 中声明了我的风格:

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

我想 link 第一个 setter 到我的组合框:

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

我尝试通过绑定设置 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 我不假装解决方案必须遵循我的 code/approach,任何方法对我来说都很好。 我只是想象有一个相对简单的解决方案我太stupid/ignorant看不到

您可以在 window 中找到所有控件并将其 属性 FontSize 设置为 ComboBox 中选定的值。

private void FontSizeComboChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (int.TryParse((sender as ComboBox).SelectedItem?.ToString(), out var selectedFontSize))
        {
            foreach (var win in System.Windows.Application.Current.Windows)
            {
                var allChildren = FindVisualChildren<Control>(win as Window);
                foreach (var c in allChildren)
                {
                    c.FontSize = selectedFontSize;
                }
            }
        }
    }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.control.fontsize?view=netframework-4.8

我找到了一个简单的解决方案。 我 post 给可能会遇到同样情况的任何人:

    <Style x:Name="ControlBaseStyle" x:Key="ControlBaseStyle" TargetType ="{x:Type Control}">
        <Setter Property="FontSize" Value="{Binding ElementName=CBFontSize, Path=SelectedValue.Content}"/>
        .
        .
        .
    </Style>

我的错误是我绑定了 ComboBox 的 SelectedValue,在我过去使用 WindowsForm 的经验中包含有效值,但在 WPF 中,ComboBox 的 SelectedValue 是一个 ComboBoxItem,因此我必须获取它的内容物品。 现在可以了:)