gpt4 book ai didi

c# - Windows Phone 8.1 上的 RadioButtons 绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:27:49 24 4
gpt4 key购买 nike

我的 RadioButton 绑定(bind)有问题房产 IsChecked .我有两个 RadioButton的在网格上,Visibility绑定(bind)到我的 View 模型上的属性。我想要实现的是总是先设置RadioButton检查状态,当网格变得可见时。
这是一些代码:

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

按照我的逻辑,它应该首先设置 RadioButton如属性 State 时检查当网格变得可见时,将进入特定状态。它工作正常,直到我达到第二个 RadioButton .然后我的绑定(bind)不起作用,当 State正在改变 我的 StateToBooleanConverter 中没有任何 react .
我阅读了很多关于单选按钮绑定(bind)问题的信息,但在我的情况下没有任何效果。
如果没有用于检查 radioButton 的新属性,是否可以这样做?我将不胜感激任何建议我如何解决这个问题。

编辑:

viewmodel 和 Converter 中有一些代码用于 IsChecked :
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;
}
}

最佳答案

假设 PageState 是一个枚举,this answer is what you're looking for.

您想要组合在一起的所有单选按钮都绑定(bind)到 ViewModel 的相同属性并且都使用相同的 ValueConverter。触发单选按钮检查/取消选中的值被传递到 ValueConverter 的 parameter属性(property)。

对于您的具体问题,EnumBooleanConverter可以直接复制粘贴到您的代码中(请务必阅读并确保您理解它)。

然后 XAML 变为

<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 EnumBooleanConverter}, ConverterParameter=RadioButtonVisible}"
Content="content 1" />

<RadioButton Grid.Row="1"
Margin="20,0"
IsChecked="{Binding State, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=*Insert enum value here*}"
Content="content 2" />


</Grid>

关于c# - Windows Phone 8.1 上的 RadioButtons 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30754880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com