C# 使用切换按钮创建选择器
我在使用mvvm的C#WPF中工作,不知道如何设计应用程序来创建选择器 我有一个包含点的类:C# 使用切换按钮创建选择器,c#,wpf,mvvm,C#,Wpf,Mvvm,我在使用mvvm的C#WPF中工作,不知道如何设计应用程序来创建选择器 我有一个包含点的类: public class MyCustomRectangle { public MyCustomPoint Point1; public MyCustomPoint Point2; public MyCustomPoint Point3; public MyCustomPoint Point4; } 我的应用程序包含一个图像。用户可以通过单击图像绘制4个点。他必须选择要
public class MyCustomRectangle
{
public MyCustomPoint Point1;
public MyCustomPoint Point2;
public MyCustomPoint Point3;
public MyCustomPoint Point4;
}
我的应用程序包含一个图像。用户可以通过单击图像绘制4个点。他必须选择要绘制的点并单击
我想将选择器作为切换按钮,绑定在我的MainViewModel上:
class MainViewModel : ViewModelBase
{
private MyCustomRectangle _myPoints;
public MyCustomRectangle MyPoints
{
get
{
return _myPoints;
}
}
public int _selectedPointIndex;
public int SelectedPointIndex
{
get
{
return _selectedPointIndex;
}
set
{
_selectedPointIndex = value;
OnPropertyChanged();
}
}
public int SelectedPoint
{
get
{
return MyPoints.GetPoint(SelectedPointIndex);
}
}
}
但我不知道如何轻松地将我的按钮绑定到视图模型。行为必须是:
- 所有按钮均未选中
- 如果用户选中按钮1,则按钮1将传递到已选中,并且SelectedPointIndex=1
- 如果用户选中按钮2,则按钮2将传递到选中,并且SelectedPointIndex=2和按钮1未选中
<ToggleButton Grid.Row="0" Grid.Column="0" Content="Point 1"/>
<ToggleButton Grid.Row="0" Grid.Column="1" Content="Point 2"/>
<ToggleButton Grid.Row="1" Grid.Column="0" Content="Point 3"/>
<ToggleButton Grid.Row="1" Grid.Column="1" Content="Point 4"/>
如何简单地绑定按钮?根据您的预期行为使用
单选按钮
,并将其样式设置为切换按钮
。使用单选按钮
可在此处找到:
定义样式:
<Style x:Key="RadioButtonLooksAsToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton"/>
使用以下样式:
<RadioButton Style="{StaticResource RadioButtonLooksAsToggleButton}" />
为什么不使用单选按钮并为其设置样式?我不是WPF方面的专家,我不知道如何执行。切换按钮在我看来更容易使用。这似乎是个好主意。如何获取选中的单选按钮?在给定链接中,它是绑定属性CurrentOption