Skip to main content
 首页 » 编程设计

c#之单选按钮绑定(bind) MVVM 应用程序

2024年08月06日33pander-it

我有一个包含 4 个单选按钮和一个按钮的 radiobutton.xaml 文件

我通过这段代码在主窗口显示了单选按钮

<ContentControl Content="{StaticResource RB}" Height="326" x:Name="select" /> 

现在我需要为单选按钮实现绑定(bind)

我无法将单选按钮和按钮绑定(bind)到 View 模型。需要在单击按钮时代表选定的单选按钮打开新窗口。

难以为单选按钮制作 V-M。不知道把绑定(bind)代码放在哪里...

<ResourceDictionary 
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                    xmlns:src="clr-namespace:DiagramDesigner"> 
 
    <GroupBox x:Key="RB" Header="Select The Architecture Modeling Style" Height="400" > 
        <StackPanel> 
            <TextBlock Text="Custom Style Architecture Modeling:" FontSize="20" 
               Margin="30 30 40 10" HorizontalAlignment="Center" /> 
            <RadioButton Content="Custome Architecture Modeling" Margin="50 0 10 10"  
                 GroupName="Standard"  /> 
            <TextBlock Text="Standard Style Architecture Modeling:" FontSize="20" 
               Margin="30 0 40 10" HorizontalAlignment="Center" /> 
            <RadioButton Content="3-Tier Architecture Modeling" Margin="50 0 10 0"  
                 GroupName="Standard" /> 
            <RadioButton Content="Client-Server Architecture Modeling"  
                 Margin="50 0 10 0" GroupName="Standard" /> 
            <RadioButton Content="Pipeline and Filter Architecture Modeling"  
                 Margin="50 0 10 0" GroupName="Standard" /> 
            <Button Margin="100 20 100 0" Width="200" HorizontalContentAlignment="Center"> 
                <Button.Content> 
                    <Grid> 
                        <Grid.RowDefinitions> 
                            <RowDefinition Height="4*"/> 
                            <RowDefinition Height="1*"/> 
                        </Grid.RowDefinitions> 
                        <TextBlock Grid.Row="1" Text="Let's Go Draw It..." VerticalAlignment="Bottom" HorizontalAlignment="Center"/> 
                    </Grid> 
                </Button.Content> 
            </Button> 
        </StackPanel> 
    </GroupBox> 
</ResourceDictionary> 

需要绑定(bind)成MVVM

请您参考如下方法:

我建议您使用 ListBox 方法而不是您提到的方法。你可以在这里找到它:
http://www.codeproject.com/Tips/807025/WPF-MVVM-Binding-for-Multiple-Radio-Buttons

如果您想保留“抽象”组(自定义和标准样式架构建模),那么我现在想到的解决方案之一是实现一个TextBox 在 ControlTemplate 中并将其绑定(bind)到 View 模型上的属性。

    <ListBox ItemsSource="{Binding RadioCollection}" SelectedItem="{Binding SelectedRadio}"> 
        <ListBox.ItemContainerStyle> 
            <Style TargetType="{x:Type ListBoxItem}"> 
                <Setter Property="Template"> 
                    <Setter.Value> 
                        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
                            <TextBlock Text="{Binding AbstractGroupHeader}" /> 
                            <RadioButton  
                                Content="{Binding Header}" ToolTip="{Binding ToolTip}" 
                                IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/> 
                        </ControlTemplate> 
                    </Setter.Value> 
                </Setter> 
                <Setter Property="Margin" Value="5"/> 
            </Style> 
        </ListBox.ItemContainerStyle> 
    </ListBox> 

View 模型基本上负责 View 的状态(例如, View 上的动画未在 View 模型中定义,但 View 模型可能会释放,如启动它)。 VM 是您定义的类,它必须实现在命名空间 System.ComponentModel 中找到的 INotifyPropertyChanged 接口(interface),以防您希望在代码中更改属性值时通知 View 。请记住,该属性必须具有 public 访问修饰符,以便绑定(bind)起作用。如果您想遵循此方法,请阅读位于我提供的链接下的文章。但是,如果您想要一个更简单的解决方案,它将适用于您的代码,那么您必须将每个单选按钮的 IsChecked 依赖属性绑定(bind)到 View 模型上的适当属性,如下所示:

    <RadioButton Content="Pipeline and Filter Architecture Modeling"  
             Margin="50 0 10 0" GroupName="Standard" IsChecked="{Binding IsPipelinedModeling}"/> 

在这种情况下,VM 看起来像这样:

public class SettingsViewModel : INotifyPropertyChanged 
{ 
    bool _isPipelinedModeling; 
    public bool IsPipelinedModeling 
    { 
        get { return _isPipelinedModeling; } 
        set 
        { 
            if (_isPipelinedModeling == value) 
                return; 
            _isPipelinedModeling = value; 
            RaisePropertyChanged(); 
        } 
    } 
 
    #region INotifyPropertyChanged implementation 
 
    public void RaisePropertyChanged([CallerMemberName]string propertyName = "") 
    { 
        if (PropertyChanged != null) 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
 
    public event PropertyChangedEventHandler PropertyChanged; 
 
    #endregion INotifyPropertyChanged implementation 
} 

要将 View 模型绑定(bind)到 View ,您可以使用“ View 优先”或“ View 模型优先”方法。我首先使用 View 。在窗口、用户控件或您正在使用的任何内容的构造函数中,添加以下代码: this.Loaded += (s, e) => { this.DataContext = new SettingsViewModel(); }; 该代码创建一个新的 View 模型对象并将其设置为窗口的 DataContext。

但是绑定(bind)到按钮有点不同,因为您必须声明一个命令。它是你自己的一个类,实现了ICommand接口(interface):

    ICommand _drawModelingArchitectureCommand; 
    public ICommand DrawModelingArchitectureCommand 
    { 
        get 
        { 
            return _drawModelingArchitectureCommand ?? (_drawModelingArchitectureCommand = new DrawTheModelingArchitectureCommand()); 
        } 
    } 
 
    public class DrawTheModelingArchitectureCommand : ICommand 
    { 
        public bool CanExecute(object parameter) 
        { 
            // the code that decides whether the button will be enabled or not 
            return true; 
        } 
 
        public event EventHandler CanExecuteChanged; 
 
        public void Execute(object parameter) 
        { 
            // the code that is executed when the button is pressed 
        } 
    } 

最后是按钮的 XAML:

    <Button Grid.Row="1" Content="Let's Go Draw It..." VerticalAlignment="Bottom" HorizontalAlignment="Center" Command="{Binding DrawTheModelingArchitecture}"/>