简体   繁体   English

Style.Trigger上的自定义DependencyProperty

[英]Custom DependencyProperty on Style.Trigger

for an UserControl I declared a DependencyProperty : 对于UserControl我声明了DependencyProperty

        public static readonly DependencyProperty DeselectedPresentationModeProperty =
        DependencyProperty.Register(
        "DeselectedPresentationMode",
        typeof(int),
        typeof(MyUserControl),
        new FrameworkPropertyMetadata(1,FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public static readonly DependencyProperty SelectedPresentationModeProperty =
        DependencyProperty.Register(
        "SelectedPresentationMode",
        typeof(int),
        typeof(MyUserControl),
        new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

Now I want to use these properties within a MultiTrigger: 现在,我想在MultiTrigger中使用这些属性:

    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}" x:Key="MyUserControlItemStyle">
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="False" />
                    <Condition Property="gui:MyUserControl.DeselectedPresentationMode" Value="0" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeIconOnly}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="False" />
                    <Condition Property="gui:MyUserControl.DeselectedPresentationMode" Value="1" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeSimple}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True" />
                    <Condition Property="gui:MyUserControl.SelectedPresentationMode" Value="0" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeExtended}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True" />
                    <Condition Property="gui:MyUserControl.SelectedPresentationMode" Value="1" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeIconOnly}" />
                </MultiTrigger.Setters>
            </MultiTrigger>
        </Style.Triggers>
    </Style>
    ...
    ...
    <ListBox ... ItemContainerStyle="{StaticResource MyUserControlItemStyle}"/>

But if I use MyUserControl another project, there is no reaction if I change my DependencyProperty . 但是,如果我在另一个项目中使用MyUserControl,则更改DependencyProperty不会有任何反应。 Whether I use xaml eg: 我是否使用xaml例如:

<Window 
...
x:Class="WpfControlsTester.MainWindow" 
Title="MainWindow">
<Grid>
    <GUI:MyUserControl Name="test"/>
    <Slider Minimum="0" Maximum="1" Value="{Binding SelectedPresentationMode, ElementName=test}" />
    <Slider Minimum="0" Maximum="1" Value="{Binding DeselectedPresentationMode, ElementName=test}" />
</Grid>

or if I use any kind of manipulation the value like this.test.DeselectedPresentationMode = 0; 或者,如果我使用任何形式的操作,则此值应为this.test.DeselectedPresentationMode = 0; on an event or smth else. 在其他事件上。

I used WPF Inspector to "debug" my Triggers, and both properties always had their default value (1). 我使用WPF检查器来“调试”触发器,并且两个属性始终具有默认值(1)。 Where is my mistake or what should I add to update my triggers? 我的错误在哪里,或者应该添加什么以更新触发器?

Edit : Changed false "MyControl to "MyUserControl" 编辑:将错误的“ MyControl更改为” MyUserControl“

You can try the MultiDatatrigger instead like this in order to get the property changes: 您可以像这样尝试MultiDatatrigger来获取属性更改:

<Style.Triggers>
                <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False" />
                        <Condition Binding="{Binding DeselectedPresentationMode, RelativeSource={RelativeSource AncestorType={x:Type MyUserControl}}}" Value="0" />
                        </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                        <Setter Property="ContentTemplate" Value="{StaticResource PresentationModeIconOnly}" />
                        </MultiDataTrigger.Setters>
                </MultiDataTrigger>
            </Style.Triggers>

Thanks 谢谢

This could just be a copy and paste error, but in your code example where you declare the Dependency Properties , your control is named MyUserControl , but in the other code examples, it is called MyControl . 可能只是复制和粘贴错误,但是在您声明Dependency Properties代码示例中,控件名为MyUserControl ,而在其他代码示例中,控件称为MyControl These names will need to match if you want this to work. 如果您希望这些名称起作用,则这些名称将需要匹配。

UPDATE >>> 更新>>>

Ok, I thought that might be the case... so, moving on... can you see the property value if you simply bind to it? 好的,我认为可能是这样...所以,继续...如果仅绑定到属性值就可以看到它吗?

<TextBlock Text="{Binding SelectedPresentationMode, ElementName=test}" />

If you can, then we can move on... usually when a binding does not work, there is a line that starts with the word Error output into the Output Window in Visual Studio. 如果可以,那么我们可以继续...通常在绑定不起作用时,在Visual Studio的“ Output Window中有一行以“ Error输出”开头。 What does your error say. 您的错误怎么说? if you have one? 如果你有一个? (You may need to turn this functionality on by going to Tools > Options > Debugging > Output Window > WPF Trace Settings and setting the Data Binding option to Warning or Error .) (您可能需要打开此功能,方法是转到工具>选项>调试>输出窗口> WPF跟踪设置,然后将Data Binding选项设置为WarningError 。)

If you have no error in the Output Window, this could be trickier to debug. 如果“输出”窗口中没有错误,则调试起来可能会比较棘手。 Let me know. 让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM