WPF 애플리케이션의 기본 글꼴을 설정하는 방법은 무엇입니까?
내 WPF 응용 프로그램에 대한 글꼴 패밀리를 정의 할 수 있기를 원합니다. 에서 참조하는 테마로 리소스 사전을 사용하는 것이 App.xaml
좋습니다. Style
다음과 같이 만들려고 시도했습니다 .
<Style TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
그러나 이것은 작동하지 않습니다. 유형을 TextBlock
대부분의 컨트롤 에서 작동 하도록 설정 하지만 이것이 적용되지 않는 몇 가지 컨트롤이 있습니다.
창에 글꼴을 설정하고 해당 창의 모든 자식 컨트롤이 글꼴을 상속하도록 할 수 있다는 것을 알고 있습니다. 그러나 모든 대화 창은 내가 원하는 것이 아닌 기본 글꼴로 돌아갈 것이라고 생각합니다.
어떤 아이디어?
Window
하위 클래스 가을 재정의하지 않는다고 가정하면 상속 된 속성 DefaultStyleKey
이므로 간단히 Window 스타일에 추가 할 수 있습니다 TextElement.FontFamilyProperty
.
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
또한 InitializeComponent
호출 후 앱 생성자에 다음을 추가해야합니다 .
FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
{
DefaultValue = FindResource(typeof(Window))
});
작동 방식 : App 개체가 초기화를 마치면 여기에 지정된 창 스타일이 모든 창에 대한 기본 스타일이됩니다.
대부분의 제안 된 솔루션이 저에게 적합하지 않았습니다. 내 간단한 해결책 :
이것을 App.xaml에 추가하십시오.
<Style TargetType="{x:Type Window}">
<Setter Property="FontSize"
Value="14" />
</Style>
MainWindow 생성자 (InitializeComponent 이후)에 다음을 추가합니다.
Style = (Style)FindResource(typeof(Window));
프로그래밍 방식으로 수행하는 한 가지 간단한 방법 :
public MainWindow()
{
this.FontFamily = new FontFamily("Segoe UI");
}
나는 이것을 찾았다 :
TextElement.FontFamilyProperty.OverrideMetadata(
typeof(TextElement),
new FrameworkPropertyMetadata(
new FontFamily("Comic Sans MS")));
TextBlock.FontFamilyProperty.OverrideMetadata(
typeof(TextBlock),
new FrameworkPropertyMetadata(
new FontFamily("Comic Sans MS")));
실제로 여기에서 다른 답변 중 일부를 결합한 완전한 XAML 솔루션을 얻을 수 있습니다.
메인 창이 호출되면 WinMain
(다른 모든 창 보다 먼저로드하는 창 ), eg라는 이름의 스타일에 대한 참조를 추가하기 만하면됩니다.WinAll
<Window x:Class="MyNamespace.WinMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="WinMain" Height="450" Width="800"
Style="{StaticResource WinAll}">
그런 다음 이렇게 스타일을 정의하세요
<Style x:Key="WinAll" TargetType="{x:Type Window}">
<Setter Property="FontFamily"
Value="Comic Sans MS" />
<Setter Property="FontSize"
Value="14" />
</Style>
다음에서이 간단한 해결 방법을 시도해보십시오 App.xaml
(코드가 필요하지 않음).
<SolidColorBrush x:Key="ForeBrush" Color="Blue" />
<Style x:Key="GenericTextStyle">
<!-- Generic control forecolor definition -->
<Setter Property="Control.Foreground" Value="{StaticResource ForeBrush}" />
<!-- Add a definition for each unworking nested control -->
<Style.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="{StaticResource ForeBrush}" />
</Style>
</Style.Resources>
</Style>
Just bind your windows style to this. Works perfectly for me. Only some properties need to be defined in the nested tree. For example the property FontSize
can be specify only in the generic section.
I don't know why is necessary to do this trick. It's strange because Label should be derived from Control. Anyone have any idea about it?
참고URL : https://stackoverflow.com/questions/3145511/how-to-set-the-default-font-for-a-wpf-application
'IT박스' 카테고리의 다른 글
git am error : "patch does not apply" (0) | 2020.11.02 |
---|---|
많은 git 저장소 관리 (0) | 2020.11.02 |
IIS 인스턴스에 디버거 연결 (0) | 2020.11.01 |
NSFetchedResultsController-문자열의 첫 글자로 생성 된 섹션 (0) | 2020.11.01 |
Java 용 "Parallel.For"? (0) | 2020.11.01 |