用户控件内复选框的 IsChecked 绑定不适合

问题描述 投票:0回答:1

在 UserControl.xaml 中:

<UserControl x:Class="OurNameSpace.SystemCalibration.Controls.PortSelector"
             xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
             xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
             xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 
             xmlns:d=http://schemas.microsoft.com/expression/blend/2008 
             xmlns:local="clr-namespace:OurNamespace.SystemCalibration.Controls"
             xmlns:converters="clr-namespace:OurNamespace.SystemCalibration.Converters"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             x:Name="portSelector"
             mc:Ignorable="d">
<Grid …..
               <StackPannel ….
<CheckBox Content="Port 4" Margin="0 5 0 5"
                      IsChecked="{Binding cb4_IsChecked, ElementName=portSelector, UpdateSourceTrigger=PropertyChanged}">
                <CheckBox.Visibility>
                    <MultiBinding Converter="{StaticResource maxPortsToVisibilityConverter}">
                        <Binding Path="MaxPorts" ElementName="portSelector"/>
                        <Binding Source="{StaticResource port4}"/>
                    </MultiBinding>
                </CheckBox.Visibility>
              </CheckBox>
</StackPanel>
</Grid>

在 usercontrol.xaml.cs 中:

public partial class PortSelector : UserControl
{
public static readonly DependencyProperty cb4_isCheckedProperty = DependencyProperty.Register(nameof(cb4_IsChecked), typeof(bool), typeof(PortSelector));

public bool cb4_IsChecked
        {
            get => (bool)GetValue(cb4_isCheckedProperty);
            set
            {
                SetValue(cb4_isCheckedProperty, value);
                UpdatedSelectedPortsString();
            }
       }
}

如果我在调用“UpdatedSelectedPortsString”的线路上放置一个中断,则断点永远不会被命中。

此用户控件托管在 TabControl 内的另一个用户控件内。当我单击该复选框时,我希望执行依赖属性的 Set 部分,但该代码似乎根本没有被调用。我什至尝试向 IsChecked 绑定添加一个假值转换器,并且转换器内的代码确实执行,但设置器不执行。如果我不绑定 IsChecked,而是添加具有依赖属性的 Command 绑定,并将命令处理程序放在同一 usercontrol.xaml.cs 代码隐藏部分类中,它也可以工作。唯一似乎不起作用的是直接绑定 IsChecked。

wpf checkbox data-binding dependency-properties
1个回答
0
投票

您必须使用 PropertyChangedCallback 委托,如下所示:

public static readonly DependencyProperty cb4_IsCheckedProperty = DependencyProperty.Register(nameof(cb4_IsChecked), typeof(bool), typeof(PortSelector), new PropertyMetadata(false, cb4_IsCheckedChangedCallback));
    public bool cb4_IsChecked
    {
        get { return (bool)GetValue(cb4_IsCheckedProperty); }
        set { SetValue(cb4_IsCheckedProperty, value); }
    }
    private static void cb4_IsCheckedChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        PortSelector uc = d as PortSelector;
        uc.UpdatedSelectedPortsString();
    }

每次 IsChacked 更改时都会调用“cb4_IsCheckedChangedCallback”方法。

如果这不起作用。此代码应该用于绑定:

IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PortSelector}}, Path=cb4_IsChecked}"
© www.soinside.com 2019 - 2024. All rights reserved.