我有一个radiobutton.xaml文件,它有4个单选按钮和一个按钮
我通过下面的代码在主窗口上显示了单选按钮
<ContentControl Content="{StaticResource RB}" Height="326" x:Name="select" />现在我需要实现单选按钮的绑定。
我不能将单选按钮和按钮绑定到视图模型。需要打开新窗口代表选定的单选按钮,点击按钮。
很难为单选按钮制作V。不知道该把绑定代码放哪儿..。
<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
发布于 2015-01-24 20:58:51
我建议您使用ListBox方法,而不是前面提到的方法。你可以在这里找到它:
http://www.codeproject.com/Tips/807025/WPF-MVVM-Binding-for-Multiple-Radio-Buttons
如果您希望保留“抽象”组(自定义和标准样式的体系结构建模),那么我现在想到的解决方案之一就是在ControlTemplate中实现一个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是您定义的类,它必须实现在命名空间System.ComponentModel中找到的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) => { this.DataContext =();};代码创建一个新的视图模型对象,并将其设置为窗口的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}"/>https://stackoverflow.com/questions/27415251
复制相似问题