MVVM中三态变量的XAML触发器

时间:2018-12-18 11:33:08

标签: c# wpf xaml

我的bool?中有一个ViewModel,它绑定到XAML中的两个RadioButtons

如果属性是RadioButton'sIsChecked,我希望将第一个True true设置为null,将第二个{{1 }}设置为倒数。我只是似乎找不到一种方法来更新RadioButton中的属性。

这是我当前无法正常使用的XAML:

ViewModel

如果我从第二个<RadioButton Name="RadioButtonDhcpConfig" Grid.Row="1" Grid.Column="1" Content="DHCP" VerticalAlignment="Center" Margin="5"> <RadioButton.Resources> <Style TargetType="RadioButton"> <Setter Property="IsChecked" Value="{Binding DhcpEnabled}"/> <Style.Triggers> <DataTrigger Binding="{Binding DhcpEnabled}" Value="{x:Null}"> <Setter Property="IsChecked" Value="True"/> </DataTrigger> </Style.Triggers> </Style> </RadioButton.Resources> </RadioButton> <!--Row 2--> <RadioButton Name="RadioButtonManualConfiguration" Grid.Row="2" Grid.Column="1" Content="Manual Configuration:" VerticalAlignment="Center" Margin="5"> <RadioButton.Resources> <Style TargetType="RadioButton"> <Setter Property="IsChecked" Value="{Binding DhcpEnabled}"></Setter> <Style.Triggers> <DataTrigger Binding="{Binding DhcpEnabled}" Value="False"> <Setter Property="IsChecked" Value="True"></Setter> </DataTrigger> <DataTrigger Binding="{Binding DhcpEnabled}" Value="{x:Null}"> <Setter Property="IsChecked" Value="False"></Setter> </DataTrigger> <DataTrigger Binding="{Binding DhcpEnabled}" Value="True"> <Setter Property="IsChecked" Value="False"></Setter> </DataTrigger> </Style.Triggers> </Style> </RadioButton.Resources> </RadioButton> 中删除了所有DataTriggers,则在单击第一个RadioButton时(如预期的那样)但上面的XAML被调用了DhcpEnabled的setter该设置员永远不会被调用。

这里的问题是,当用户单击第二个RadioButton且找不到解决方法时,我需要确保将DhcpEnabled属性设置为false

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

以下是您第一个按钮的转换器:

ArrayList<>

然后您可以像这样在public class Bool2NullConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return value == null; } if (value is bool?) { return (value as bool?).Value; } return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } } 中使用它:
RadioButton
您现在要做的就是反转第一个转换器的逻辑,使其成为反相器,并将其应用于第二个<RadioButton IsChecked="{Binding DhcpEnabled, Converter={StaticResource bool2Null}}">。它将使XAML也变得更加干净。

答案 1 :(得分:0)

这应该有效。对于第一个单选按钮,您可以将默认值设置为true,然后使用触发器在值为false时对其进行更改。对于第二个控件,您只需将其绑定为与第一个控件相反。

# -*- coding: utf-8 -*-

from moviepy.editor import *

# Load Intro
intro = (VideoFileClip("spoken-perfect-white.mp4", audio=False)
           #.subclip(4,5)
           )

audioclip1 = AudioFileClip("output1.wav")
audioclip2 = AudioFileClip("output2.wav")

# MAKE THE CREDITS SCREEN

txt_credits = """
hydroxydesoxycorticosterone
"""

credits = (TextClip(txt_credits, color='black',
            font="Keep-Calm-Medium", fontsize=35, kerning=-2,
            interline=-1, bg_color='white', size=intro.size)
          .set_duration(60)
          )

final = concatenate_videoclips([intro, credits])

final.write_videofile("final.mp4", fps=intro.fps,
                      audio_bitrate="1000k", bitrate="4000k", threads=2)

答案 2 :(得分:0)

在视图模型中添加一个反向属性(或使用转换器):

private bool? dhcpEnabled;

public bool? DhcpEnabled
{
    get => dhcpEnabled;
    set
    {
        if (dhcpEnabled != value)
        {
            dhcpEnabled = value;
            OnPropertyChanged(nameof(DhcpEnabled));
            OnPropertyChanged(nameof(DhcpDisabled));
        }
    }
}

public bool? DhcpDisabled
{
    get => DhcpEnabled == false;
    set => DhcpEnabled = !value;
}

然后在XAML中:

请特别注意GroupName属性。

<RadioButton x:Name="RadioButtonDhcpConfig"
             GroupName="MyDhcp"
             Grid.Row="1" Grid.Column="1" Content="DHCP"
             VerticalAlignment="Center" Margin="5"
             IsChecked="{Binding DhcpEnabled}"/>
<RadioButton x:Name="RadioButtonManualConfiguration"
             GroupName="MyDhcp"
             Grid.Row="2" Grid.Column="1"
             Content="Manual Configuration:"
             VerticalAlignment="Center" Margin="5"
             IsChecked="{Binding DhcpDisabled}"/>