我使用ListView
的动态DataTemplate
如下:
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,1" BorderBrush="#ccc">
<RadioButton GroupName="MyRadioButtonGroup">
<StackPanel Orientation="Horizontal" Margin="0,0,0,5" Width="{Binding ActualWidth, ElementName=CheckoutGrid}">
<TextBlock Text="{Binding Path=Test1, StringFormat='{}{0} - '}" />
<TextBlock Text="{Binding Path=Test2, StringFormat='{} test {0} - '}" />
<TextBlock Text="{Binding Path=Test, StringFormat='{} test {0}'}" />
</StackPanel>
</RadioButton>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
我希望禁用一个按钮,直到其中一个RadioButton IsChecked
:
<RadioButton Padding="15,10" VerticalAlignment="Center">
<RadioButton.Style>
<Style TargetType="RadioButton" BasedOn="{StaticResource styleToggleButton4}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyRadioButtonGroup, Path=IsChecked}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
<TextBlock FontWeight="SemiBold" FontSize="14" Margin="0">NEXT</TextBlock>
</RadioButton>
所以问题是我不知道如何正确绑定RadioButton GroupName="MyRadioButtonGroup"
。您将在DataTrigger
上方看到我正在尝试Binding="{Binding ElementName=MyRadioButtonGroup, Path=IsChecked}" Value="False"
,但这对我不起作用,因为很明显它是GroupName
而不是x:Name
。
有关如何正确处理此问题的任何建议?如果可能的话,我宁愿在前端处理这个问题。
答案 0 :(得分:0)
您可以将单选按钮绑定到命令,然后使用isChecked作为参数。然后绑定到基于该布尔值的布尔值。我认为这可能更像是一种解决方法。
<RadioButton
Command="{Binding MyCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"
GroupName="radio" />
<RadioButton
Command="{Binding MyCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"
GroupName="radio" />
然后在代码隐藏或VM中,它需要实现ICommand接口和INotifyPropertyChanged
public bool IsChecked
{
get { return isChecked;}
set
{
isChecked = value;
OnPropertyChanged();
}
}
private void OnMyCommand(object obj)
{
if(obj is bool)
{
IsChecked = (bool)obj;
}
}
答案 1 :(得分:0)
一个组没有可以绑定的IsChecked
属性。
如果您没有源属性来跟踪是否至少有一个已检查RadioButton
和&#34;想要在前端处理此问题&#34; - 我想在视图中意味着 - 您可以编写一些代码,只需将Tag
的{{1}}属性设置为ListView
/ true
,具体取决于是否存在{ {1}}已选中。这是一个单行:
false
RadioButton