【问题标题】:c# wpf in a listbox doing binding with checkbox to an object within another objectc# wpf在列表框中使用复选框绑定到另一个对象中的对象
【发布时间】:2021-02-15 20:26:38
【问题描述】:

我有一个列表框,它由如下所示的元素组成。

 class ItemForListbox
    {
        public string path { get; set; }
        public string file { get; set; }
        public InstallationPackageChoice choices { get; set; }
        public KeepKill keepKill { get; set; }
    }

choices 和 keepKill 是对象。

我的列表框 XAML 有这个 DataTemplate

<ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                        <Button Name="btnPackageVersion" Style="{StaticResource ListBoxButtonFormat}" Content ="Package version" Click="btnInstallPackage_Click" HorizontalAlignment="Right"/>

                        <TextBlock Name="txtBlkPath" Text="{Binding path}" FontSize="10" Width="500" Height="20" Margin="10,10,0,0"/>
                        <Button Name="btnFilterPath" Style="{StaticResource ListBoxButtonFormat}" Content ="Filter path" Click="btnFilterpath_Click" HorizontalAlignment="Right"/>

                        <TextBlock Name="txtBlkFile" Text="{Binding file}" FontSize="10" Width="150" Height="20" Margin="10,10,0,0"/>
                        <Button Name="btnFilterfile" Style="{StaticResource ListBoxButtonFormat}" Content="Filter file" Click="btnFilterfile_Click" HorizontalAlignment="Right"/>

                        <CheckBox Name="chkBxKeep" Content="Keep" VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="True" Checked="chkBxKeep_Checked"/>
                        <CheckBox Name="chkBxKill" Content="Kill" VerticalAlignment="Center" HorizontalAlignment="Right" Checked="chkBxKill_Checked"/>

                        <Separator  Name="MySeparator" 
                                        Height="3"
                                        Width="Auto"
                                        HorizontalAlignment="Stretch"
                                        VerticalAlignment="Bottom"                   
                                        Background="Red" />
                    </StackPanel>
                </DataTemplate>
</ItemsControl.ItemTemplate>

两个checkbox底层数据是Listbox元素对象中的killKeep对象。

问题是我想要两个复选框的单选按钮功能,所以当我选中一个复选框时,另一个取消选中。我不使用单选按钮的原因是我根本不了解它们的绑定。但现在我发现我也不知道如何使用复选框进行此绑定。

我希望有人可以帮助我。

【问题讨论】:

    标签: c# wpf xaml checkbox data-binding


    【解决方案1】:

    您可以使用绑定转换器来绑定复选框的IsChecked 属性:

    <Window.Resources>
        <local:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
    </Window.Resources>
    ...
    <CheckBox Name="chkBxKeep" Content="Keep" VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="True" Checked="chkBxKeep_Checked"/>
    <CheckBox Name="chkBxKill" Content="Kill" VerticalAlignment="Center" HorizontalAlignment="Right" Checked="chkBxKill_Checked"
    IsChecked="{Binding ElementName=chkBxKeep, Path=IsChecked, Converter={StaticResource InverseBooleanConverter}}"/>
    

    转换器

    public class InverseBooleanConverter : IValueConverter
    {
        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is bool)
            {
                return !(bool)value;
            }
    
            return true;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Convert(value, targetType, parameter, culture);
        }
    
        #endregion
    }
    

    【讨论】:

    • 感谢您的快速回答,请检查一下......:)
    • @KimSandberg 伟大
    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2014-01-15
    相关资源
    最近更新 更多