【问题标题】:Radio Button Binding MVVM Application单选按钮绑定 MVVM 应用程序
【发布时间】:2014-12-11 04:12:19
【问题描述】:

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

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

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

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

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

难以为单选按钮制作 V-M。不知道绑定代码到底应该放在哪里...

<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>

需要将其绑定为 MVVM

【问题讨论】:

标签: c# wpf mvvm binding


【解决方案1】:

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

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

    <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>

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

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

在这种情况下,虚拟机看起来像这样:

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
}

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

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

    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}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 2011-01-18
    • 2019-07-02
    • 1970-01-01
    • 2016-08-28
    • 2021-06-27
    • 2010-10-27
    相关资源
    最近更新 更多