USERCORTROL内部的WPF依赖性Property绑定到另一个Usercontrol,不更新Coerce

发布于 2025-02-03 07:35:00 字数 4215 浏览 2 评论 0原文

我有一个带有contentPresenter的家长USercontrol(NewsymbologyCombobox),该内容显示了两个控件之一。

NewsymbologyCombobox控制具有“选择”依赖性属性,

    <ContentPresenter x:Name="Presenter" >
        <!-- Presentations -->
        <ContentPresenter.Resources>
            <DataTemplate x:Key="EditablePresenter">
                <editableCombobox:EditableSymbologyComboBox SelectedOption="{Binding SelectedOption, ElementName=UserControl}" />
            </DataTemplate>
            <DataTemplate x:Key="DropdownPresenter">
                <dropdownCombobox:DropdownSymbologyComboBox SelectedOption="{Binding SelectedOption, ElementName=UserControl}" />
            </DataTemplate>
        </ContentPresenter.Resources>
        <!-- Triggers -->
        <ContentPresenter.Style>
            <Style TargetType="{x:Type ContentPresenter}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsEditable, ElementName=UserControl}" Value="False">
                        <Setter Property="ContentTemplate" Value="{StaticResource DropdownPresenter}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsEditable, ElementName=UserControl}" Value="True">
                        <Setter Property="ContentTemplate" Value="{StaticResource EditablePresenter}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentPresenter.Style>
    </ContentPresenter>
/// <summary>
        /// Property for the symbology setter to use on the plotted elements.
        /// </summary>
        public static readonly DependencyProperty SelectedOptionProperty = DependencyProperty.Register("SelectedOption", typeof(ISymbologyOption), typeof(NewSymbologyComboBox), new FrameworkPropertyMetadata(new UserDefinedSymbologyOption(""), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is ISymbologyOption option)
            {
                System.Diagnostics.Debug.Print(option.DisplayName); //This is not called on the coerced initial change.
            }

            
        }

editablesymbologycombobox和下拉杂志Bobobox也具有依赖性property selectedOption。

        public static readonly DependencyProperty SelectedOptionProperty = DependencyProperty.Register("SelectedOption", typeof(ISymbologyOption), typeof(DropdownSymbologyComboBox), new FrameworkPropertyMetadata(new UserDefinedSymbologyOption(""), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyChangedCallback, CoerceValueCallback));

下拉杂志Bobobox具有一个强制回调,如果不在可用选项中,则修改该值。

private static object CoerceValueCallback(DependencyObject d, object basevalue)
        {
            if (d is DropdownSymbologyComboBox comboBox)
            {
                if (!comboBox._isInitialized)
                    comboBox.PopulateOptions();

                if (basevalue is UserDefinedSymbologyOption userDefinedSymbology)
                {
                    var matchingOption = comboBox.AvailableOptions.FirstOrDefault(option => string.Equals(userDefinedSymbology.DisplayName, option.DisplayName, StringComparison.InvariantCultureIgnoreCase));
                    if (matchingOption != null)
                        return matchingOption;

                    return comboBox.AvailableOptions.FirstOrDefault();
                }
            }

            return basevalue;
        }

在我的窗口上,我在视图模型中显示我的默认值如图所示

        <combobox:NewSymbologyComboBox Margin="10 5 0 0" SelectedOption="{Binding PlotOptions.SelectedSymbology}" IsEditable="False"/>
        <TextBlock Text="{Binding PlotOptions.SelectedSymbology.DisplayName}"/>

,设置为“我的第一个选择”,该选项在选项中不可用,应将其胁迫到列表中的第一个项目... Coerce功能找到这并将其设置为第一个可用选项...

我遇到的问题是我的窗口没有随着此更改而更新。它显示“我的第一个选择”,直到我手动进行ComboBox进行更改。

任何帮助都很棒!谢谢

I have a parent usercontrol(NewSymbologyComboBox) with a contentpresenter that displays one of two controls.

The NewSymbologyComboBox control has a "SelectedOption" dependency property

    <ContentPresenter x:Name="Presenter" >
        <!-- Presentations -->
        <ContentPresenter.Resources>
            <DataTemplate x:Key="EditablePresenter">
                <editableCombobox:EditableSymbologyComboBox SelectedOption="{Binding SelectedOption, ElementName=UserControl}" />
            </DataTemplate>
            <DataTemplate x:Key="DropdownPresenter">
                <dropdownCombobox:DropdownSymbologyComboBox SelectedOption="{Binding SelectedOption, ElementName=UserControl}" />
            </DataTemplate>
        </ContentPresenter.Resources>
        <!-- Triggers -->
        <ContentPresenter.Style>
            <Style TargetType="{x:Type ContentPresenter}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsEditable, ElementName=UserControl}" Value="False">
                        <Setter Property="ContentTemplate" Value="{StaticResource DropdownPresenter}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsEditable, ElementName=UserControl}" Value="True">
                        <Setter Property="ContentTemplate" Value="{StaticResource EditablePresenter}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentPresenter.Style>
    </ContentPresenter>
/// <summary>
        /// Property for the symbology setter to use on the plotted elements.
        /// </summary>
        public static readonly DependencyProperty SelectedOptionProperty = DependencyProperty.Register("SelectedOption", typeof(ISymbologyOption), typeof(NewSymbologyComboBox), new FrameworkPropertyMetadata(new UserDefinedSymbologyOption(""), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is ISymbologyOption option)
            {
                System.Diagnostics.Debug.Print(option.DisplayName); //This is not called on the coerced initial change.
            }

            
        }

Both the EditableSymbologyComboBox and the DropdownSymbologyComboBox have a dependencyproperty SelectedOption in them as well.

        public static readonly DependencyProperty SelectedOptionProperty = DependencyProperty.Register("SelectedOption", typeof(ISymbologyOption), typeof(DropdownSymbologyComboBox), new FrameworkPropertyMetadata(new UserDefinedSymbologyOption(""), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyChangedCallback, CoerceValueCallback));

The DropDownSymbologyComboBox has a coerce callback which modifies the value if it is not in the available options.

private static object CoerceValueCallback(DependencyObject d, object basevalue)
        {
            if (d is DropdownSymbologyComboBox comboBox)
            {
                if (!comboBox._isInitialized)
                    comboBox.PopulateOptions();

                if (basevalue is UserDefinedSymbologyOption userDefinedSymbology)
                {
                    var matchingOption = comboBox.AvailableOptions.FirstOrDefault(option => string.Equals(userDefinedSymbology.DisplayName, option.DisplayName, StringComparison.InvariantCultureIgnoreCase));
                    if (matchingOption != null)
                        return matchingOption;

                    return comboBox.AvailableOptions.FirstOrDefault();
                }
            }

            return basevalue;
        }

On My window I am displaying the selection as shown

        <combobox:NewSymbologyComboBox Margin="10 5 0 0" SelectedOption="{Binding PlotOptions.SelectedSymbology}" IsEditable="False"/>
        <TextBlock Text="{Binding PlotOptions.SelectedSymbology.DisplayName}"/>

My default value in my view model is set to "My First Selection" which is not available in the options and should be coerced to the first item in the list... The coerce function finds this and sets it to the first available option...

The problem I am having is that my window is not updating with this change. It is displaying "My First Selection" until I make a change in the combobox manually.

Any help would be great! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文