我已经更改了单选按钮的模板。但是组名属性不起作用

时间:2013-12-01 16:57:18

标签: c# wpf radio-button

我为我的radiobutton创建了一个controltemplate。 groupName属性似乎不起作用。我的两个单选按钮都被检查了。此外,绑定仅在加载时发生,之后属性也不会更新。我在这里错过了什么吗? 请在下面找到xaml代码和资源文件代码:

XAML代码:

<UserControl.Resources>
    <ResourceDictionary>
        <local:IntegerToBooleanConverter x:Key="IntegerToBooleanConverter"/>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyApplication;component/Styles/RadioButtonShadowEffect.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <StackPanel>
        <Label Name="Header" Content="Get from resource file" HorizontalAlignment="Stretch" Margin="15,15,15,0" FontWeight="Bold"/>
        <Separator Margin="15,15,15,0"/>
        <Label Name="ContentTitle" Content="Get from resource file" HorizontalAlignment="Stretch" Margin="30,15,15,0"/>
        <Grid Margin="35,15,15,0" Width="600" HorizontalAlignment="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <StackPanel Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Left">
                <RadioButton Template="{StaticResource MultiLineRadioButtonTemplate}" Name="RadioSingleBore" HorizontalAlignment="Left"  IsChecked="{Binding BoreCount, Mode=TwoWay, Converter ={StaticResource IntegerToBooleanConverter}, ConverterParameter= 1}" GroupName="BoreCount">
                        <StackPanel>
                            <Border Name="RadioSingleBoreBorder" Background="LightGray" Margin="0,15,0,0" Height="300" Width="290" CornerRadius="5" HorizontalAlignment="Left">
                                <StackPanel>
                                    <Label Name="RadioSingleBoreLabel" Content="Get from rosource file" Margin="15"/>
                                    <Image Name="SingleBoreImage" Stretch="Fill" Height="40" Margin="17,70,17,15" Source="SomeImage"/>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                </RadioButton>
            </StackPanel>
            <StackPanel Grid.Column="1" Orientation="Vertical" HorizontalAlignment="Left">
                <RadioButton Template="{StaticResource MultiLineRadioButtonTemplate}" Name="RadioDoubleBore"  HorizontalAlignment="Left" IsChecked="{Binding BoreCount, Mode=TwoWay, Converter ={StaticResource IntegerToBooleanConverter}, ConverterParameter= 2}"  GroupName="BoreCount">
                    <StackPanel>
                        <Border Name="RadioDoubleBoreBorder" Background="LightGray" Margin="0,15,0,0" Height="300" Width="290" CornerRadius="5" HorizontalAlignment="Left">
                        <StackPanel>
                            <Label Name="RadioDoubleBoreLabel" Content="Get from rosource file" Margin="15"/>
                            <Image Name="DoubleBoreImage" Stretch="Fill" Height="110" Margin="17,35,17,15" Source="SomeImage"/>
                        </StackPanel>
                    </Border>
                    </StackPanel>
                </RadioButton>
            </StackPanel>
        </Grid>
    </StackPanel>
</Grid>

resoucre文件如下:     

<ControlTemplate x:Key="MultiLineRadioButtonTemplate" TargetType="{x:Type RadioButton}">
    <RadioButton IsChecked="{TemplateBinding IsChecked}">
        <TextBlock>
        <LineBreak />
        <InlineUIContainer>
            <ContentPresenter Margin="-15,0,0,0" 
                              Content="{TemplateBinding ContentPresenter.Content}"
                              ContentTemplate="{TemplateBinding ContentPresenter.ContentTemplate}"/>
        </InlineUIContainer>
        </TextBlock>
    </RadioButton>
</ControlTemplate>

我的转换器代码如下:

public class IntegerToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int source = 1;
        int target = 1;

        int.TryParse(value.ToString(), out source);
        int.TryParse(parameter.ToString(), out target);

        return (source == target);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? parameter : Binding.DoNothing;
    }
}

1 个答案:

答案 0 :(得分:0)

ControlTemplate RadioButton还有RadioButtonRadioButton。我想,您的组名不会被模板中定义的嵌套<ControlTemplate x:Key="MultiLineRadioButtonTemplate" TargetType="{x:Type RadioButton}"> <RadioButton IsChecked="{TemplateBinding IsChecked}" GroupName="{TemplateBinding GroupName}"> <TextBlock> <LineBreak /> <InlineUIContainer> <ContentPresenter Margin="-15,0,0,0" Content="{TemplateBinding ContentPresenter.Content}" ContentTemplate="{TemplateBinding ContentPresenter.ContentTemplate}"/> </InlineUIContainer> </TextBlock> </RadioButton> </ControlTemplate> 继承。

将ControlTemplate更新为:

TemplateBinding

基本上,此ControlTemplate通过{{1}}在RadioButton上设置GroupName属性。

我已经测试了这段代码,似乎工作正常。