글쎄, 실제로 동적이지는 않습니다. 적어도 런타임에는 변경되지 않습니다.XAML 이미지 소스는 콘텐츠를 기반으로 동적으로 설정됩니다.
아이디어는 버튼이 있고 각 이미지에는 고유 한 이미지 (아이콘 32x32)가 있습니다. 버튼들은 모두 ControlTemplate을 엉망으로 만드는 스타일을 공유합니다. 그래서 각각의 이미지는 정상적인 것과 정상적인 것의 2 가지 색상을 가지고 있습니다.
내가 이미지의 소스 경로를 선언하면 거의 동일하므로 DRY (반복하지 말 것)라는 사실을 알게되었습니다. 원본 경로 (예 : 이미지 파일의 이름)의 일부로 버튼 이름이나 다른 속성을 사용할 수 있다면 어떨까요? 좋은 프로그래밍이 될 것입니다.
문제는 내가 XAML, WPF 및 아마도 프로그래밍을 처음 사용하기 때문에 어떻게해야할지 모르겠다. 이 코드를 뒤에 또는 변환기 (변환기에 대해 좀 더 읽을 것 같아요) 일종의 것이라고 생각합니다. 여기에 코드의 비트입니다 (이 작동하지 않습니다하지만 그것은 당신에게 희망 일반적인 생각() 제공) :
<Style x:Key="ButtonPanelBigButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="ButtonBorder"
Height="78"
MaxWidth="70"
MinWidth="50"
BorderThickness="0.5"
BorderBrush="Transparent"
CornerRadius="8" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Here I wan't to put in the Name property of the button because there is a picture there to match -->
<Image x:Name="ButtonIcon" Source="..\Images\Icons\32x32\Blue\{Binding Name}.png"
Margin="4"
Height="32"
Width="32"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<TextBlock Grid.Row="1"
Padding="5,2,5,2"
TextWrapping="Wrap"
Style="{StaticResource MenuText}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ContentPresenter ContentSource="Content" />
</TextBlock>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True" >
<Setter TargetName="ButtonIcon" Property="Source" Value="..\Images\Icons\32x32\Green\user.png" /> <!-- Same Here -->
<Setter TargetName="ButtonBorder" Property="BorderBrush" Value="{StaticResource SecondColorBrush}" />
<Setter TargetName="ButtonBorder" Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" Opacity="0.5">
<GradientStop Color="{StaticResource MainColor}" Offset="1" />
<GradientStop Color="{StaticResource SecondColor}" Offset="0.5" />
<GradientStop Color="{StaticResource MainColor}" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
가 잘하면 내가이 함께 갈거야 누군가가 그렇게 나를 도울 수있을 어디서 얻을 수를 내 코드는 멋지다. 그리고 DRY (지금 나는 스스로 반복하고있다 !!!).
이 작업을 수행하려고했지만 경로 및 {0} –
OK가 매력처럼 작동하므로 문자열 리터럴 및 마크 업 확장에 문제가있는 경우이 StringFormat = {} ..과 같은 리터럴 때문에 속성을 하나 변경해야합니다. \\ 이미지 \\ 아이콘 \\ 32x32 \\ 녹색 \\ {0} .png} " –