c# WPF 上图下文字radiobutton样式,且可动态生成radiobutton,搭配触摸滚动条

上图下文字radiobutton样式(如下图),根据文件夹下图片动态生成radiobutton,搭配触摸滚动条

c# WPF 上图下文字radiobutton样式,且可动态生成radiobutton,搭配触摸滚动条

 

代码:

StyleDictionary.xaml:
    <Style x:Key="selectRadioButton" TargetType="{x:Type RadioButton}">
        <Setter Property="Width" Value="215"></Setter>
        <Setter Property="Height" Value="280"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Grid x:Name="grid" VerticalAlignment="Center">
                        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Border x:Name="border" CornerRadius="10" BorderThickness="1" BorderBrush="{StaticResource blueColor}">
                                <Image Name="img" Source="{Binding Path=radionButtonSource}" Stretch="Fill" Width="215" Height="200"/>
                            </Border>
                            <TextBlock Name="content" Text="{TemplateBinding Content}" Style="{StaticResource TextBlock}"/>
                        </StackPanel>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="border" Property="BorderThickness" Value="2"/>
                            <Setter TargetName="content" Property="Style" Value="{StaticResource SelectedTextBlock}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

MainWindow.xaml:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <loc:TouchableScrollViewer x:Name="viewer" VerticalAlignment="Center" HorizontalAlignment="Center" HorizontalScrollBarVisibility ="Hidden" VerticalScrollBarVisibility="Disabled">
            <DockPanel Name="dockPanel" />
        </loc:TouchableScrollViewer>
    </Grid>

MainWindow.xaml.cs:

   public class RadionButtonImage
    {
        public ImageSource _radionButtonSource;

        public ImageSource radionButtonSource
        {
            get { return _radionButtonSource; }
            set { _radionButtonSource = value; }
        }
    }

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DirectoryInfo root = new DirectoryInfo("./Image");
            FileInfo[] files = root.GetFiles();
            foreach(var path in files)
            {
                RadioButton btn = SetRadioButton(path.Name, path.FullName);
                dockPanel.Children.Add(btn);
            }
        }
        private RadioButton SetRadioButton(string content, string path)
        {
            RadioButton btn = new RadioButton();
            btn.Checked += new RoutedEventHandler(RadioButton_Check);
            btn.Margin = new Thickness(10);
            btn.Style = this.FindResource("selectRadioButton") as Style;
            btn.Content = content;
            RadionButtonImage image = new RadionButtonImage();
            image.radionButtonSource = new BitmapImage(new Uri(path));
            btn.DataContext = image;
            return btn;
        }

 

完整代码下载:https://download.****.net/download/hijane/12318894