[英]Binding ImageSource changeable at DesignTime
The problem: I would like to create a button template in XAML
where I bind the image source to a content presenter content box or anything similar. 问题:我想在
XAML
中创建一个按钮模板,在其中将图像源绑定到内容演示者内容框或任何类似内容。
Required parameters for the solution: I understand that for a standard button I'm able to add a stackpanel or grid to the button, insert an image into said container and call it a day. 解决方案所需的参数:我知道对于标准按钮,我可以向按钮添加堆栈面板或网格,将图像插入到所述容器中并命名为“ day”。 However, I'm not a fan of the windows 'chrome' look nor the blue mouse over, click, and focus effects.
但是,我既不喜欢Windows的“ chrome”外观,也不喜欢蓝色的鼠标悬停,单击和聚焦效果。 What I'm attempting to do is create a new button template (editing a copy of the default button template and removing the chrome container) that has the capability to be a base template with the option of changing images as I add more buttons.
我要尝试做的是创建一个新的按钮模板(编辑默认按钮模板的副本并删除chrome容器),该模板具有作为基本模板的功能,并且可以选择在添加更多按钮时更改图像。
Things I've tried and their results: 我尝试过的事情及其结果:
<Button x:Name="btn">
<StackPanel>
<Image source="imgsource.png"/>
</StackPanel>
</Button>
Result: Has the button chrome effect. 结果:具有按钮镀铬效果。 Can be a fall back if nothing else possible but I don't like it.
如果没有其他可能,可以退后,但我不喜欢它。
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bContainer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Height="{TemplateBinding Height}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Width="{TemplateBinding Width}">
<Grid x:Name="gBody" Background="#FF3C3C3C"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Button Style="{DynamicResource ButtonStyle}">
<Grid>
<Image Source="imgsource.png"/>
</Grid>
</Button>
Result: The image doesn't show up. 结果:图像不显示。 I'm thinking this is due to the button custom template, but my knowledge on this is limited so I haven't delved too deep.
我认为这是由于按钮自定义模板引起的,但是我对此的了解有限,因此我并没有太深入地研究。
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bContainer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Height="{TemplateBinding Height}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Width="{TemplateBinding Width}">
<Image x:Name="iImage" Source="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Left" Height="16" VerticalAlignment="Top" Width="16"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Button Style="{DynamicResource ButtonStyle}">
<Grid>
<Image Source="imgsource.png"/>
</Grid>
</Button>
Result: The image only shows up when entering the FULL path to the image. 结果:仅在输入图像的完整路径时显示图像。 I'm not sure if I'm correct, but wouldn't this cause problems when deploying the program?
我不确定我是否正确,但是在部署程序时这不会引起问题吗?
I'm kind of surprised your third example works - even with the full path. 我很惊讶您的第三个示例的工作-即使是完整路径。 Just at a glance it does not seem like it should work.
乍一看似乎不行。 Your second attempt is close but you do not have a site to bind the button's content to.
您的第二次尝试已结束,但是您没有将按钮的内容绑定到的站点。
Look at your button: 看一下您的按钮:
<Button Style="{DynamicResource ButtonStyle}">
<Grid>
<Image Source="imgsource.png"/>
</Grid>
</Button>
The Grid
is the Content
(the grid is not necessary - you can just put the Image
there). Grid
是Content
(网格不是必需的-您可以将Image
放在此处)。 Looking at your template you can see there is nothing in there that will pick up the content. 查看您的模板,您会发现其中没有任何内容可以拾取内容。 Try adding a content presenter to your template:
尝试将内容演示者添加到模板中:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bContainer"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Height="{TemplateBinding Height}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
Width="{TemplateBinding Width}">
<ContentPresenter x:Name="contentPresenter"
Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused"
Value="true"/>
<Trigger Property="ToggleButton.IsChecked"
Value="true"/>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.