单选按钮绑定MVVM应用程序

时间:2014-12-11 04:12:20

标签: c# wpf mvvm binding

我有一个radiobutton.xaml文件,它有4个单选按钮和一个按钮

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

<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

1 个答案:

答案 0 :(得分:3)

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

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

    <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是您定义的类,它必须实现命名空间INotifyPropertyChanged中的System.ComponentModel接口,以防您在更改代码中的属性值时通知视图。请记住,该属性必须具有public访问修饰符,才能使绑定生效。如果您想要遵循此方法,请阅读我给出的链接下面的文章。但是,如果您想要一个更简单的解决方案,它将与您的代码一起使用,那么您必须将每个单选按钮的IsChecked依赖项属性绑定到视图模型上的相应属性,如下所示:

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

要将视图模型绑定到视图,您可以使用“先查看”或“先查看模型”方法。我先使用视图。在窗口的构造函数,用户控件或您正在使用的任何内容中,添加以下代码:             this.Loaded + =(s,e)=&gt;             {                 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}"/>