本文介绍了单选按钮和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试设置的图像,而不是子弹为我的单选按钮。我已经10单选按钮,但我不能设置我FishImg所有。只有RB我只显示前imgFish点击。
我的风格
<风格的TargetType =单选>
< setter属性=内容VALUE ={DynamicResource imgFish}/>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型单选}>
<网格和GT;
<图像源=图像/ empty.pngWIDTH =24HEIGHT =24/>
< ContentPresenter />
< /网格和GT;
< /控件模板>
< /Setter.Value>
< /二传手>
< Style.Triggers>
<触发属性=器isCheckedVALUE =真>
< setter属性=内容VALUE ={DynamicResource imgHero}/>
< /触发>
<触发属性=器isCheckedVALUE =FALSE>
< setter属性=内容VALUE ={DynamicResource imgFish}/>
< /触发>
< /Style.Triggers>
< /样式和GT;
我的XAML
<单选Grid.Column =2Grid.Row =1NAME =hvP1/>
<单选Grid.Column =2Grid.Row =2NAME =hvP2/>
<单选Grid.Column =2Grid.Row =3NAME =hvP3/>
将;单选Grid.Column =2Grid.Row =4名称=hvP4/>
<单选Grid.Column =2Grid.Row =5NAME =hvP5/>
<单选Grid.Column =2Grid.Row =6NAME =hvP6/>
<单选Grid.Column =2Grid.Row =7NAME =hvP7/>
<单选Grid.Column =2Grid.Row =8NAME =hvP8/>
<单选Grid.Column =2Grid.Row =9NAME =hvP9/>
<单选Grid.Column =2Grid.Row =10NAME =hvP10/>
解决方案
最后,我今天发现它,即使我很确定我昨天......试了一下可能是累了。
下面是一个有效方法。
<窗口x:类=WpfApplication2.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X = http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525>
< Window.Resources>
<图像源=图像/ on.pngX:键=imgOn/>
<图像源=图像/ off.pngX:键=imgOff/>
<风格的TargetType =单选>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型单选}>
<网格和GT;
<图像源=图像/ off.pngWIDTH =32HEIGHT =32/>
< ContentPresenter />
< /网格和GT;
< /控件模板>
< /Setter.Value>
< /二传手>
< Style.Triggers>
<触发属性=器isCheckedVALUE =真>
< setter属性=内容VALUE ={DynamicResource imgOn}/>
< /触发>
<触发属性=器isCheckedVALUE =FALSE>
< setter属性=内容VALUE ={DynamicResource imgOff}/>
< /触发>
< /Style.Triggers>
< /样式和GT;
< /Window.Resources>
<&StackPanel的GT;
<单选按钮名称=RB1WIDTH =32HEIGHT =32保证金=10/>
<单选按钮名称=RB2WIDTH =32HEIGHT =32保证金=10/>
<单选按钮名称=RB3WIDTH =32HEIGHT =32保证金=10/>
<单选按钮名称=RB4WIDTH =32HEIGHT =32保证金=10/>
< / StackPanel的>
I try to set up images instead of bullet for my RadioButtons. I've 10 RadioButtons but I can't setup my FishImg for all. Only the rb I clicked just before displays the imgFish.
My Style
<Style TargetType="RadioButton">
<Setter Property="Content" Value="{DynamicResource imgFish}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid>
<Image Source="Images/empty.png" Width="24" Height="24" />
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="{DynamicResource imgHero}"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content" Value="{DynamicResource imgFish}"/>
</Trigger>
</Style.Triggers>
</Style>
My XAML
<RadioButton Grid.Column="2" Grid.Row="1" Name="hvP1" />
<RadioButton Grid.Column="2" Grid.Row="2" Name="hvP2" />
<RadioButton Grid.Column="2" Grid.Row="3" Name="hvP3" />
<RadioButton Grid.Column="2" Grid.Row="4" Name="hvP4" />
<RadioButton Grid.Column="2" Grid.Row="5" Name="hvP5" />
<RadioButton Grid.Column="2" Grid.Row="6" Name="hvP6" />
<RadioButton Grid.Column="2" Grid.Row="7" Name="hvP7" />
<RadioButton Grid.Column="2" Grid.Row="8" Name="hvP8" />
<RadioButton Grid.Column="2" Grid.Row="9" Name="hvP9" />
<RadioButton Grid.Column="2" Grid.Row="10" Name="hvP10" />
解决方案
Finally I found it today even if I was pretty sure I tried it yesterday... may be tired.
Here a working solution
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Image Source="Images/on.png" x:Key="imgOn"/>
<Image Source="Images/off.png" x:Key="imgOff"/>
<Style TargetType="RadioButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid>
<Image Source="Images/off.png" Width="32" Height="32"/>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="{DynamicResource imgOn}"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content" Value="{DynamicResource imgOff}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<RadioButton Name="rb1" Width="32" Height="32" Margin="5"/>
<RadioButton Name="rb2" Width="32" Height="32" Margin="5"/>
<RadioButton Name="rb3" Width="32" Height="32" Margin="5"/>
<RadioButton Name="rb4" Width="32" Height="32" Margin="5"/>
</StackPanel>
这篇关于单选按钮和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!