반응형
XAML에서 변수를 정의하려면 어떻게 해야 하나요?
XAML에는 다음 2개의 버튼이 있습니다.
<Button Content="Previous"
Margin="10,0,0,10"/>
<Button Content="Next"
Margin="0,0,10,10"/>
"10"을 변수로 정의하여 한 곳에서 변경할 수 있도록 하려면 다음과 같이 하십시오.
유사 코드:
<variable x:key="theMargin"/>
<Button Content="Previous"
Margin="{Variable theMargin},0,0,{Variable theMargin}"/>
<Button Content="Next"
Margin="0,0,{Variable theMargin},{Variable theMargin}"/>
이것을 시험해 보세요.
삼파일의 머리에 추가하다
xmlns:System="clr-namespace:System;assembly=mscorlib"
그런 다음 리소스 섹션에 다음을 추가합니다.
<System:Double x:Key="theMargin">2.35</System:Double>
마지막으로 여백에 두께를 사용합니다.
<Button Content="Next">
<Button.Margin>
<Thickness Top="{StaticResource theMargin}" Left="0" Right="0"
Bottom ="{StaticResource theMargin}" />
</Button.Margin>
</Button>
int, char, string, Date Time 등 많은 시스템 유형을 정의할 수 있습니다.
주의: 맞습니다...더 나은 테스트를 해야 했는데..동작하도록 코드로 변경하다
Sorskoot의 답변과 마찬가지로 사용할 두께 리소스를 추가하여 각 여백 방향을 독립적으로 정의할 수 있습니다.
<UserControl.Resources>
<Thickness x:Key="myMargin" Top="5" Left="10" Right="10" Bottom ="5"></Thickness>
</UserControl.Resources>
그런 다음 두께를 여백으로 사용합니다.
<Button Content="Next" Margin="{StaticResource myMargin}"/>
로서 값을 추가해 보는 것은 어떨까요?
Resources.Add("theMargin", 10);
그러면 다음과 같은 값을 얻을 수 있습니다.
<Button Content="Previous"
Margin="{StaticResource theMargin},0,0,{StaticResource theMargin}"/>
<Button Content="Next"
Margin="0,0,{StaticResource theMargin},{StaticResource theMargin}"/>
Initialize Component를 사용하거나 INotify를 사용하기 전에 호출해야 합니다.그 후 Property Changed 인터페이스
.NET Core @Sorskoot의 답변은 유효했지만 경고가 발생했습니다.대신 다음 네임스페이스 선언을 사용했습니다.
xmlns:system="clr-namespace:System;assembly=System.Runtime"
언급URL : https://stackoverflow.com/questions/877304/how-can-i-define-a-variable-in-xaml
반응형
'programing' 카테고리의 다른 글
키가 첫 번째 열에 없는 Excel VLOOKUP (0) | 2023.04.17 |
---|---|
목록의 누적 합계는 어떻게 찾나요? (0) | 2023.04.17 |
오브젝트 내의 속성별로 목록을 정렬하는 방법 (0) | 2023.04.17 |
디렉토리의 파일을 루프 오버하고 경로를 변경하고 파일 이름에 접미사를 추가하는 방법 (0) | 2023.04.17 |
Xcode 4는 "Attaching to (app name)"에 행업합니다. (0) | 2023.04.17 |