【发布时间】:2015-08-25 14:51:36
【问题描述】:
我在绑定RadioButton 属性IsChecked 时遇到了一些问题。我在网格上有两个RadioButton,Visibility 绑定到我的视图模型上的一个属性。我想要实现的是始终将第一个 RadioButton 设置为 Checked 状态,当网格变得可见时。
这是一些代码:
<Grid Visibility="{Binding State, Converter={StaticResource VisibilityConverter}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<RadioButton Grid.Row="0"
Margin="20,0"
IsChecked="{Binding State, Converter={StaticResource StateToBooleanConverter}}"
Content="content 1" />
<RadioButton Grid.Row="1"
Margin="20,0"
Content="content 2" />
</Grid>
按照我的逻辑,当属性State 进入特定状态时,它应该首先将RadioButton 设置为已检查,当网格变为可见时。它工作正常,直到我达到第二个RadioButton。然后我的绑定不起作用,当State 发生变化时,我的StateToBooleanConverter 没有任何反应。
我阅读了很多关于单选按钮绑定问题的信息,但在我的情况下没有任何效果。
如果没有用于检查 radioButton 的新属性,是否可以这样做?我将不胜感激任何建议我如何解决这个问题。
编辑:
IsChecked 有一些来自 viewmodel 和 Converter 的代码:
public class MainViewModel : ViewModel
{
public MainViewModel
{
this.ChangeState = new RelayCommand(this.ChangeStateExecute);
}
public PageState State
{
get
{
return this.state;
}
set
{
if (this.state != value)
{
this.state = value;
base.RaisePropertyChanged();
}
}
}
public RelayCommand ChangeState { get; private set; }
private void ChangeStateExecute()
{
this.State = PageState.RadioButtonsVisible;
}
}
public class StateToBooleanConverter : Converter
{
protected override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var state = (PageState)value;
var result = state == PageState.RadioButtonsVisible;
return result;
}
}
【问题讨论】:
-
请附上你的cs代码。
-
State是否实现了INotifyPropertyChanged? -
@mike-eason 是的,确实如此。
-
@lloyd 查看我的帖子,我添加了一些代码
-
如何工作 base.RaisePropertyChanged(); ??
标签: c# xaml mvvm radio-button windows-phone-8.1