반응형
코드에서 WPF 레이블의 Style 속성을 설정하시겠습니까?
App.xaml에는 다음 코드가 있습니다.
<Application.Resources>
<Style x:Key="LabelTemplate" TargetType="{x:Type Label}">
<Setter Property="Height" Value="53" />
<Setter Property="Width" Value="130" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Margin" Value="99,71,0,0" />
<Setter Property="VerticalAlignment" Value= "Top" />
<Setter Property="Foreground" Value="#FFE75959" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontSize" Value="40" />
</Style>
</Application.Resources>
이것은 내 레이블에 대한 일반 템플릿을 제공하기 위한 것입니다.
메인 XAML 코드에는 다음과 같은 코드 줄이 있습니다.
<Label Content="Movies" Style="{StaticResource LabelTemplate}" Name="label1" />
하지만 코드를 통해 Style 속성을 초기화하고 싶습니다.시도해 본 결과:
label1.Style = new Style("{StaticResource LabelTemplate}");
그리고.
label1.Style = "{StaticResource LabelTemplate}";
두 솔루션 모두 유효하지 않습니다.
도움을 주시면 감사하겠습니다 :).
당신은 그 스타일을 어디서 얻으려고 합니까?비밀번호?
다음과 같이 기록해야 합니다.
코드백 상태인 경우:
Style style = this.FindResource("LabelTemplate") as Style;
label1.Style = style;
당신이 다른 곳에 있다면,
Style style = Application.Current.FindResource("LabelTemplate") as Style;
label1.Style = style;
아래쪽 노트: 이름 지정 안 함Style을 키워드로 하여Template당신은 결국 혼란스럽게 될 것입니다.Style그리고 aTemplate그리고 그것들은 두 가지 다른 개념이기 때문에 당신은 하지 말아야 합니다.
null 스타일 결과를 확인하십시오. 그렇지 않으면 슬플 것입니다. ...(style!= null).스타일 = 스타일;
오래된 질문일 수도 있지만 W10 UWP 앱을 사용하려면 각 개체의 리소스 모음 또는 응용 프로그램 개체의 리소스 모음을 사용해야 합니다.
KeyValuePair<object,object> styl = this.Resources
.FirstOrDefault(x => x.Key == "MyStyleTemplateName");
if (styl.Value != null)
Style MyStyle = (Style)styl.Value;
MyStyleTemplateName을 이 리소스로 정의해야 하는 위치
언급URL : https://stackoverflow.com/questions/10686917/setting-the-style-property-of-a-wpf-label-in-code
반응형
'programing' 카테고리의 다른 글
| 절대적이지만 상위에 상대적인 위치 (0) | 2023.04.29 |
|---|---|
| 'Microsoft' 유형의 COM 개체를 캐스트할 수 없습니다.사무실. 인터럽트.Excel.ApplicationClass'에서 'Microsoft'로 이동합니다.사무실. 인터럽트.Excel.응용 프로그램' (0) | 2023.04.29 |
| Meteor: 클라이언트에서 Mongo로 파일 업로드 vs 파일 시스템 vs GridFS (0) | 2023.04.29 |
| Visual Studio 2015는 구문 강조가 아닌 면도기 또는 IntelliSense (0) | 2023.04.29 |
| mongore restore 명령으로 기존 레코드를 바꾸시겠습니까? (0) | 2023.04.29 |