交叉绑定复选框-isChecked#1-> isEnabled#2

时间:2018-06-27 08:04:04

标签: c# xaml binding

我有一个ListBox,它从模型中的列表中获取布尔元素,并将它们表示为复选框。在构建项目之后,第二个复选框isEnabled设置为false。如果我在调试的第二个复选框中修改(例如,剪切并粘贴相同的转换器)绑定,则绑定开始正常工作。我也有一个全局复选框,它列出了listBox中所有复选框的modyfi isChecked属性。如果我设置globalCheckbox#2,则将所有listBox_checkBoxes#2设置为true,并将所有listBox_checkBoxes#1 isEnabled属性设置为false

XAML:

<ListBox x:Name="ListBox_assent" SelectedIndex="-1" Grid.Row="2" ItemsSource="{Binding Path=FullDataAssetList.List}" IsSynchronizedWithCurrentItem="True" Height="Auto">
<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource MaterialDesignListBoxItem}">
        <Setter Property="Margin" Value="2"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Focusable" Value="False" />
        <EventSetter Event="RequestBringIntoView" Handler="ListBoxItem_RequestBringIntoView"/>
    </Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Grid.Row="0" Opacity="{Binding Path=SkipAssentTemp, Converter={StaticResource BoolToOpacity}}">
            <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="3">
                <CheckBox  x:Name="chbx_Assent" HorizontalContentAlignment="Left" FlowDirection="RightToLeft" ToolTip="Skip" IsChecked="{Binding SkipAssent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding SkipAssentTemp, Converter={StaticResource InverseBoolean}}" LostFocus="chbx_Assent_LostFocus" Background="#FFCB0000"/>
                <TextBlock FontSize="16" Text=" / "  VerticalAlignment="Center"/>
                <CheckBox x:Name="chbx_AssentTemp" HorizontalContentAlignment="Left" FlowDirection="RightToLeft" ToolTip="Skip temp." IsChecked="{Binding SkipAssentTemp, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  IsEnabled="{Binding SkipAssent, Converter={StaticResource InverseBoolean}}" LostFocus="chbx_AssentTemp_LostFocus" Background="#FFCBA300"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

转换器:

public class InverseBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (targetType == typeof(bool) || targetType == typeof(bool?))
        {
            if ((bool?)value == true)
            {
                return false;
            }
            else
            if ((bool?)value == false)
            {
                return true;
            }
            return null;

        }
        else
        {
            throw new InvalidOperationException("The target must be a boolean");
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

 }

在构建后如何修复绑定以获得完整功能?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 xaml中的DataTrigger被覆盖了isEnabled属性