Setter.Target give me an error with “RelativePanel.AlignHorizontalCenterWithPanel”

北战南征 提交于 2019-12-20 03:52:21

问题


I am develop an UWP app, and I am using Template10. I have an TextBlock, that in VisualStateNarrow I want it RelativePanel.AlignVerticalCenterWithPanel="True" and in NormalMinWidth I want RelativePanel.AlignHorizontalCenterWithPanel="True" but I can not do this!

<VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="AdaptiveVisualStateGroup">
                    <VisualState x:Name="VisualStateNarrow">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="{StaticResource NarrowMinWidth}"/>
                        </VisualState.StateTriggers>
                        <VisualState.Setters>

                           <Setter Target="TextBlock.RelativePanel.AlignVerticalCenterWithPanel="True"/>

                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="VisualStateNormal">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="{StaticResource NormalMinWidth}"/>
                        </VisualState.StateTriggers>
                        <VisualState.Setters>

                          <Setter Target="TextBlock.RelativePanel.AlignHorizontalCenterWithPanel="True"/>

                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="VisualStateWide">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="{StaticResource WideMinWidth}"/>
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

In XAML editor give me this error: "An Exception was thrown." - "Cannot resolve TargetProperty RelativePanel.AlignHorizontalCenterWithPanel on specified object."

I have many Setter.Target and they all work! Example of my Setter.Target:

<Setter Target="TextBlock.Margin" Value="8"/>
<Setter Target="TextBlock.Width" Value="200"/>
<Setter Target="TextBlock.Height" Value="200"/>

回答1:


For attached properties you have to add brackets to in setters as follows:

Element.(Grid.Row)
Element.(ToolTipService.ToolTip)

So your code will look like this:

<Setter Target="TextBlock.(RelativePanel.AlignVerticalCenterWithPane‌​l)" Value="True" />



回答2:


Here is a way to create setters for adaptive triggers without writing a single line of code. Since VS provides neither IntelliSense nor error warning for writting them, this helps prevent bugs that are hard to diagnose.

  1. Go to the States panel, click to activate the visual state that you want to add setters to. A red dot will apear next to the name of this visual state.
  2. Once it's activated, go to the Objects and Timeline panel and select the element that you want to interact within this state. In your case, select the TextBlock element.
  3. Go to the Properties panel, either expand the RelativePanel section or search for "relative" in the search box, once the properties come up, simply select the ones you want to change.

That's all! Feel free to check out the gif demo below too.



来源:https://stackoverflow.com/questions/44475251/setter-target-give-me-an-error-with-relativepanel-alignhorizontalcenterwithpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!