【问题标题】:Cross-binding checkboxes - isChecked #1 -> isEnabled #2交叉绑定复选框 - isChecked #1 -> isEnabled #2
【发布时间】:2018-12-06 01:02:27
【问题描述】:

我有一个 ListBox,它从我的模型中的列表中获取布尔元素并将它们表示为复选框。在构建项目之后,第二个复选框 isEnabled 设置为 false。如果我在调试的第二个复选框中修改(例如剪切和粘贴相同的转换器)绑定,则绑定开始正常工作。此外,我有一个全局复选框,它 modyfi isChecked listBox 中所有复选框的属性。如果我设置 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();
    }

 }

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

【问题讨论】:

    标签: c# xaml binding


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2017-06-19
      • 2019-03-07
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多