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

我是XAML和WPF的新手。 我的目的是通过ComboBox更改出现在窗口中的所有“文本”控件的所有FontSize。

ComboBoxFontSize

我在窗口中声明了自己的风格:

       <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,我不假装解决方案必须遵循我的代码/方法,任何方法对我来说都很好。 我只是想象有一个相对简单的解决方案,我太愚蠢/无知了

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

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

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

,

我找到了一个简单的解决方案。 我将其发布给可能遇到相同情况的任何人:

if (!item.Result)

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

本文链接:https://www.f2er.com/3035190.html

大家都在问