【问题标题】:How to access Control Template controls outside using XAML?如何使用 XAML 在外部访问控制模板控件?
【发布时间】:2017-02-21 17:53:06
【问题描述】:

我想使用在另一个 NumericUpDown DatatTrigger 中的 ControlTemplate 内设计的 NumercUpDown 值,以根据条件设置其最大值。

代码-1

<ControlTemplate x:Key="OrderInfo" TargetType="ContentControl">

            <TextBlock Grid.Row="0" Grid.Column="0" Style="{StaticResource TextBlockStyle}">Limit Price:</TextBlock>
            <i:NumericUpDown Grid.Row="0" Grid.Column="1" x:Name="Price" i:Skin.IsPrice="True" RoundingDecimalPlaces="{Binding Source={StaticResource PriceFormat}, Path=MaxDecimalPlaces}" DisplayFormat="{Binding Source={StaticResource PriceFormat}, Path=StringFormat}" Minimum="0" Increment="{Binding Path=PriceIncrement.Value, TargetNullValue=1}" IncrementCount="{Binding Path=PriceIncrementCount.Value}">
                <i:NumericUpDown.Style>
                    <Style TargetType="i:NumericUpDown" BasedOn="{StaticResource BasicStyle}">
                        <Setter Property="Value" Value="{Binding Path=Price.Value, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Price.IsAvailable}" Value="False">
                                <Setter Property="Value" Value="{Binding Path=Price.EstimatedPrice, Mode=OneWay}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </i:NumericUpDown.Style>
            </i:NumericUpDown>

        </Grid>
    </ControlTemplate>

想在下面的数据触发器中使用上面的价格元素

<i:NumericUpDown Grid.Row="0" Grid.Column="3" x:Name="CompletionPrice" 
                                 Value="{Binding Path=ExternalAlgoProperties[(i:Description)CompletionPrice].Value, 
                    ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" IsEnabled="True"
                                 RoundingDecimalPlaces="0" Increment="1" Minimum="0">
                    <i:NumericUpDown.Style>
                        <Style TargetType="i:NumericUpDown" BasedOn="{StaticResource BasicStyle}">
                            <Setter Property="Maximum" Value="0"></Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Side.Code,ConverterParameter={x:Static i:SideCodes.Sell}, Converter={StaticResource EqualsConverter},UpdateSourceTrigger=PropertyChanged}" Value="True">
                                    <Setter Property="Maximum" Value="{Binding ElementName=Price,Path=Text,UpdateSourceTrigger=PropertyChanged}"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </i:NumericUpDown.Style>

                </i:NumericUpDown>

【问题讨论】:

  • 您不能引用 viewmodel Price 属性而不是尝试交叉引用 XAML 模板内容吗?
  • 可能是一个很好的解决方案。谢谢

标签: wpf xaml


【解决方案1】:

使用祖先绑定访问

            <DataTrigger
                    Binding="{Binding
                        RelativeSource={RelativeSource
                            Mode=FindAncestor,
                            AncestorType={x:Type i:NumericUpDown }},
                            Path=value}"
                    Value="True">
                //Set property
            </DataTrigger>

【讨论】:

  • @ragvan 我应该在哪里提供元素名称?我必须将它绑定在 setter 属性“最大值”值中
  • @KrishOnline 你不需要提供你可以像上面一样使用的元素名称。如果你想在你的元素中设置,只需提及 Target="ElementName"
猜你喜欢
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多