Scrollbar Template wift InvalidOperationExceoption

  • WPF

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von PadreSperanza.

    Scrollbar Template wift InvalidOperationExceoption

    Hey Leute,

    ich stehe mal wieder auf dem Schlauch. Ich habe eine ListView, in welcher per default eine Scrollbar eingebaut ist. Diese funktioniert auch wirklich hervorragend. Nun wollte ich das Aussehen der Scrollbar anpassen und habe mir deshalb den Style von Microsoft docs.microsoft.com/de-de/dotne…w=netframeworkdesktop-4.8 geholt und wollte ihn nach meinen Bedürfnissen anpassen. Rein optisch funktioniert das auch ohne Probleme( Siehe Screenshots). Wenn ich nun aber einen Style überschreibe, dann das Programm starte habe ich zwei Merkwürdigkeiten: 1. Wenn ich auf dem Scrollbar -> auf dem Track-Part klicke, wird genauso gescrollt, wie es sein sollte. 2. Wenn ich auf einen der beiden Pfeile klicke (also oben und unten von dem Trackpart), erhalte ich eine Fehlermeldung, die leider nur sehr kryptische und spärliche Informationen liefert.

    Deshalb meine Fragen:
    1. Jemand eine Ahnung, woran das liegen könnte? Ich werde aus dem Fehler nicht schlau.
    2. Weiß jemand, wie ich bei diesem Fehler weiter vorangehen könnte oder wie ich ihn deuten muss?
    3. Warum kann ein rein optischer Style plötzlich ein Fehlverhalten auslösen... es sei denn ich hätte etwas vergessen oder rausgelöscht - hab aber nur die Farben angepasst.
    Bilder
    • ListviewMitStyle.png

      12,82 kB, 205×478, 65 mal angesehen
    • ListviewOhneStyle.png

      12,3 kB, 198×476, 67 mal angesehen
    • IOExceptioon.png

      7,39 kB, 441×291, 67 mal angesehen
    • Hint.png

      20,51 kB, 204×475, 67 mal angesehen
    Hast du den Style so wie er auf der Seite angegeben ist übernommen?
    Also bei mir funktioniert er einwandfrei, ohne Änderungen.
    Was genau hast du für Werte geändert?

    Edit: Auch wenn ich die Farben ändere funktioniert noch alles einwandfrei.
    Du hast wohl unbeabsichtigt was anderes Verändert oder gelöscht.

    Spoiler anzeigen

    XML-Quellcode

    1. ​<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    3. <Color x:Key="BorderMediumColor">Lightblue</Color>
    4. <Color x:Key="BorderDarkColor">Blue</Color>
    5. <Color x:Key="BorderLightColor">#FFCCCCCC</Color>
    6. <Color x:Key="ControlLightColor">Gray</Color>
    7. <Color x:Key="ControlMediumColor">red</Color>
    8. <Color x:Key="ControlPressedColor">#FF211AA9</Color>
    9. <Color x:Key="GlyphColor">Black</Color>
    10. <Color x:Key="DisabledForegroundColor">#FF888888</Color>
    11. <Style x:Key="ScrollBarLineButton"
    12. TargetType="{x:Type RepeatButton}">
    13. <Setter Property="SnapsToDevicePixels"
    14. Value="True" />
    15. <Setter Property="OverridesDefaultStyle"
    16. Value="true" />
    17. <Setter Property="Focusable"
    18. Value="false" />
    19. <Setter Property="Template">
    20. <Setter.Value>
    21. <ControlTemplate TargetType="{x:Type RepeatButton}">
    22. <Border x:Name="Border"
    23. Margin="1"
    24. CornerRadius="2"
    25. BorderThickness="1">
    26. <Border.BorderBrush>
    27. <LinearGradientBrush StartPoint="0,0"
    28. EndPoint="0,1">
    29. <LinearGradientBrush.GradientStops>
    30. <GradientStopCollection>
    31. <GradientStop Color="{DynamicResource BorderMediumColor}"
    32. Offset="0.0" />
    33. <GradientStop Color="{DynamicResource BorderDarkColor}"
    34. Offset="1.0" />
    35. </GradientStopCollection>
    36. </LinearGradientBrush.GradientStops>
    37. </LinearGradientBrush>
    38. </Border.BorderBrush>
    39. <Border.Background>
    40. <LinearGradientBrush StartPoint="0,0"
    41. EndPoint="0,1">
    42. <LinearGradientBrush.GradientStops>
    43. <GradientStopCollection>
    44. <GradientStop Color="{DynamicResource ControlLightColor}"/>
    45. <GradientStop Color="{DynamicResource ControlMediumColor}"
    46. Offset="1.0" />
    47. </GradientStopCollection>
    48. </LinearGradientBrush.GradientStops>
    49. </LinearGradientBrush>
    50. </Border.Background>
    51. <VisualStateManager.VisualStateGroups>
    52. <VisualStateGroup x:Name="CommonStates">
    53. <VisualState x:Name="Normal" />
    54. <VisualState x:Name="MouseOver" />
    55. <VisualState x:Name="Pressed">
    56. <Storyboard>
    57. <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
    58. Storyboard.TargetProperty="(Panel.Background).
    59. (GradientBrush.GradientStops)[1].(GradientStop.Color)">
    60. <EasingColorKeyFrame KeyTime="0"
    61. Value="{StaticResource ControlPressedColor}" />
    62. </ColorAnimationUsingKeyFrames>
    63. </Storyboard>
    64. </VisualState>
    65. <VisualState x:Name="Disabled">
    66. <Storyboard>
    67. <ColorAnimationUsingKeyFrames Storyboard.TargetName="Arrow"
    68. Storyboard.TargetProperty="(Shape.Fill).
    69. (SolidColorBrush.Color)">
    70. <EasingColorKeyFrame KeyTime="0"
    71. Value="{StaticResource DisabledForegroundColor}" />
    72. </ColorAnimationUsingKeyFrames>
    73. </Storyboard>
    74. </VisualState>
    75. </VisualStateGroup>
    76. </VisualStateManager.VisualStateGroups>
    77. <Path x:Name="Arrow"
    78. HorizontalAlignment="Center"
    79. VerticalAlignment="Center"
    80. Data="{Binding Content,
    81. RelativeSource={RelativeSource TemplatedParent}}" >
    82. <Path.Fill>
    83. <SolidColorBrush Color="{DynamicResource GlyphColor}"/>
    84. </Path.Fill>
    85. </Path>
    86. </Border>
    87. </ControlTemplate>
    88. </Setter.Value>
    89. </Setter>
    90. </Style>
    91. <Style x:Key="ScrollBarPageButton"
    92. TargetType="{x:Type RepeatButton}">
    93. <Setter Property="SnapsToDevicePixels"
    94. Value="True" />
    95. <Setter Property="OverridesDefaultStyle"
    96. Value="true" />
    97. <Setter Property="IsTabStop"
    98. Value="false" />
    99. <Setter Property="Focusable"
    100. Value="false" />
    101. <Setter Property="Template">
    102. <Setter.Value>
    103. <ControlTemplate TargetType="{x:Type RepeatButton}">
    104. <Border Background="Transparent" />
    105. </ControlTemplate>
    106. </Setter.Value>
    107. </Setter>
    108. </Style>
    109. <Style x:Key="ScrollBarThumb"
    110. TargetType="{x:Type Thumb}">
    111. <Setter Property="SnapsToDevicePixels"
    112. Value="True" />
    113. <Setter Property="OverridesDefaultStyle"
    114. Value="true" />
    115. <Setter Property="IsTabStop"
    116. Value="false" />
    117. <Setter Property="Focusable"
    118. Value="false" />
    119. <Setter Property="Template">
    120. <Setter.Value>
    121. <ControlTemplate TargetType="{x:Type Thumb}">
    122. <Border CornerRadius="2"
    123. Background="{TemplateBinding Background}"
    124. BorderBrush="{TemplateBinding BorderBrush}"
    125. BorderThickness="1" />
    126. </ControlTemplate>
    127. </Setter.Value>
    128. </Setter>
    129. </Style>
    130. <ControlTemplate x:Key="VerticalScrollBar"
    131. TargetType="{x:Type ScrollBar}">
    132. <Grid>
    133. <Grid.RowDefinitions>
    134. <RowDefinition MaxHeight="18" />
    135. <RowDefinition Height="0.00001*" />
    136. <RowDefinition MaxHeight="18" />
    137. </Grid.RowDefinitions>
    138. <Border Grid.RowSpan="3"
    139. CornerRadius="2"
    140. Background="#F0F0F0" />
    141. <RepeatButton Grid.Row="0"
    142. Style="{StaticResource ScrollBarLineButton}"
    143. Height="18"
    144. Command="ScrollBar.LineUpCommand"
    145. Content="M 0 4 L 8 4 L 4 0 Z" />
    146. <Track x:Name="PART_Track"
    147. Grid.Row="1"
    148. IsDirectionReversed="true">
    149. <Track.DecreaseRepeatButton>
    150. <RepeatButton Style="{StaticResource ScrollBarPageButton}"
    151. Command="ScrollBar.PageUpCommand" />
    152. </Track.DecreaseRepeatButton>
    153. <Track.Thumb>
    154. <Thumb Style="{StaticResource ScrollBarThumb}"
    155. Margin="1,0,1,0">
    156. <Thumb.BorderBrush>
    157. <LinearGradientBrush StartPoint="0,0"
    158. EndPoint="1,0">
    159. <LinearGradientBrush.GradientStops>
    160. <GradientStopCollection>
    161. <GradientStop Color="{DynamicResource BorderLightColor}"
    162. Offset="0.0" />
    163. <GradientStop Color="{DynamicResource BorderDarkColor}"
    164. Offset="1.0" />
    165. </GradientStopCollection>
    166. </LinearGradientBrush.GradientStops>
    167. </LinearGradientBrush>
    168. </Thumb.BorderBrush>
    169. <Thumb.Background>
    170. <LinearGradientBrush StartPoint="0,0"
    171. EndPoint="1,0">
    172. <LinearGradientBrush.GradientStops>
    173. <GradientStopCollection>
    174. <GradientStop Color="{DynamicResource ControlLightColor}"
    175. Offset="0.0" />
    176. <GradientStop Color="{DynamicResource ControlMediumColor}"
    177. Offset="1.0" />
    178. </GradientStopCollection>
    179. </LinearGradientBrush.GradientStops>
    180. </LinearGradientBrush>
    181. </Thumb.Background>
    182. </Thumb>
    183. </Track.Thumb>
    184. <Track.IncreaseRepeatButton>
    185. <RepeatButton Style="{StaticResource ScrollBarPageButton}"
    186. Command="ScrollBar.PageDownCommand" />
    187. </Track.IncreaseRepeatButton>
    188. </Track>
    189. <RepeatButton Grid.Row="2"
    190. Style="{StaticResource ScrollBarLineButton}"
    191. Height="18"
    192. Command="ScrollBar.LineDownCommand"
    193. Content="M 0 0 L 4 4 L 8 0 Z" />
    194. </Grid>
    195. </ControlTemplate>
    196. <ControlTemplate x:Key="HorizontalScrollBar"
    197. TargetType="{x:Type ScrollBar}">
    198. <Grid>
    199. <Grid.ColumnDefinitions>
    200. <ColumnDefinition MaxWidth="18" />
    201. <ColumnDefinition Width="0.00001*" />
    202. <ColumnDefinition MaxWidth="18" />
    203. </Grid.ColumnDefinitions>
    204. <Border Grid.ColumnSpan="3"
    205. CornerRadius="2"
    206. Background="#F0F0F0" />
    207. <RepeatButton Grid.Column="0"
    208. Style="{StaticResource ScrollBarLineButton}"
    209. Width="18"
    210. Command="ScrollBar.LineLeftCommand"
    211. Content="M 4 0 L 4 8 L 0 4 Z" />
    212. <Track x:Name="PART_Track"
    213. Grid.Column="1"
    214. IsDirectionReversed="False">
    215. <Track.DecreaseRepeatButton>
    216. <RepeatButton Style="{StaticResource ScrollBarPageButton}"
    217. Command="ScrollBar.PageLeftCommand" />
    218. </Track.DecreaseRepeatButton>
    219. <Track.Thumb>
    220. <Thumb Style="{StaticResource ScrollBarThumb}"
    221. Margin="0,1,0,1">
    222. <Thumb.BorderBrush>
    223. <LinearGradientBrush StartPoint="0,0"
    224. EndPoint="1,0">
    225. <LinearGradientBrush.GradientStops>
    226. <GradientStopCollection>
    227. <GradientStop Color="{DynamicResource BorderLightColor}"
    228. Offset="0.0" />
    229. <GradientStop Color="{DynamicResource BorderDarkColor}"
    230. Offset="1.0" />
    231. </GradientStopCollection>
    232. </LinearGradientBrush.GradientStops>
    233. </LinearGradientBrush>
    234. </Thumb.BorderBrush>
    235. <Thumb.Background>
    236. <LinearGradientBrush StartPoint="0,0"
    237. EndPoint="0,1">
    238. <LinearGradientBrush.GradientStops>
    239. <GradientStopCollection>
    240. <GradientStop Color="{DynamicResource ControlLightColor}"
    241. Offset="0.0" />
    242. <GradientStop Color="{DynamicResource ControlMediumColor}"
    243. Offset="1.0" />
    244. </GradientStopCollection>
    245. </LinearGradientBrush.GradientStops>
    246. </LinearGradientBrush>
    247. </Thumb.Background>
    248. </Thumb>
    249. </Track.Thumb>
    250. <Track.IncreaseRepeatButton>
    251. <RepeatButton Style="{StaticResource ScrollBarPageButton}"
    252. Command="ScrollBar.PageRightCommand" />
    253. </Track.IncreaseRepeatButton>
    254. </Track>
    255. <RepeatButton Grid.Column="2"
    256. Style="{StaticResource ScrollBarLineButton}"
    257. Width="18"
    258. Command="ScrollBar.LineRightCommand"
    259. Content="M 0 0 L 4 4 L 0 8 Z" />
    260. </Grid>
    261. </ControlTemplate>
    262. <Style x:Key="{x:Type ScrollBar}"
    263. TargetType="{x:Type ScrollBar}">
    264. <Setter Property="SnapsToDevicePixels"
    265. Value="True" />
    266. <Setter Property="OverridesDefaultStyle"
    267. Value="true" />
    268. <Style.Triggers>
    269. <Trigger Property="Orientation"
    270. Value="Horizontal">
    271. <Setter Property="Width"
    272. Value="Auto" />
    273. <Setter Property="Height"
    274. Value="18" />
    275. <Setter Property="Template"
    276. Value="{StaticResource HorizontalScrollBar}" />
    277. </Trigger>
    278. <Trigger Property="Orientation"
    279. Value="Vertical">
    280. <Setter Property="Width"
    281. Value="18" />
    282. <Setter Property="Height"
    283. Value="Auto" />
    284. <Setter Property="Template"
    285. Value="{StaticResource VerticalScrollBar}" />
    286. </Trigger>
    287. </Style.Triggers>
    288. </Style>
    289. </ResourceDictionary>
    Rechtschreibfehler betonen den künstlerischen Charakter des Autors.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Akanel“ ()

    ja, ich hatte den Style von der Seite kopiert und dann fleißig geändert. Ich meine, dass ich ausschließlich Farbwerte geändert habe für Background und BorderBrush. Und einen Bereich mit <Border.Background>...</Border.Background> hatte ich weggelöscht. Ich hab das gerade nochmal kopiert und da geht tatsächlich auch bei mir alles wieder. Ich muss mich nachher mal dran setzen und schauen, was ich umgeschrieben habe, das diesen Fehler auslöst. Vielen Dank schon mal, für die Idee, wie ich hier rangehen kann :)
    Naja, wenn du deinen (abgeänderten Style) hier nicht postest können wir schwer sehen wo der Fehler liegt.

    Grüße
    SAscha
    If _work = worktype.hard Then Me.Drink(Coffee)
    Seht euch auch meine Tutorialreihe <WPF Lernen/> an oder abonniert meinen YouTube Kanal.

    ## Bitte markiere einen Thread als "Erledigt" wenn deine Frage beantwortet wurde. ##

    Nofear23m schrieb:

    Naja, wenn du deinen (abgeänderten Style) hier nicht postest können wir schwer sehen wo der Fehler liegt.
    Ja, Schande auf mein Haupt. Das hatte ich tatsächlich vergessen. An Bilder und Beschreibung hatte ich diesmal geschaut

    Nun dank des Ansatzes von Akanel habe ich nun den Style nochmal komplett neu gemacht und nach jeder Änderung geschaut, wann dieser Fehler geworfen wird:

    Es liegt an diesem Schnipsel, den ich gelöscht hatte:

    XML-Quellcode

    1. ​ <Border.Background>
    2. <LinearGradientBrush StartPoint="0,0"
    3. EndPoint="0,1">
    4. <LinearGradientBrush.GradientStops>
    5. <GradientStopCollection>
    6. <GradientStop Color="{DynamicResource ControlLightColor}"/>
    7. <GradientStop Color="{DynamicResource ControlMediumColor}"
    8. Offset="1.0" />
    9. </GradientStopCollection>
    10. </LinearGradientBrush.GradientStops>
    11. </LinearGradientBrush>
    12. </Border.Background>


    Jetzt stellte sich mir eine Zeit lang die Frage, warum das einen Fehler wirft beim Drücken. Ich fürchte, das liegt daran, dass der Style ja noch weiter geht mit dem VisualStateManager und der möchte auf dem Border einen LinearGradiantStop ändern, den ich allerdings gelöscht hatte:

    XML-Quellcode

    1. ​<VisualStateManager.VisualStateGroups>
    2. <VisualStateGroup x:Name="CommonStates">
    3. <VisualState x:Name="Normal" />
    4. <VisualState x:Name="MouseOver" />
    5. <VisualState x:Name="Pressed">
    6. <Storyboard>
    7. <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
    8. Storyboard.TargetProperty="(Panel.Background).
    9. (GradientBrush.GradientStops)[1].(GradientStop.Color)">
    10. <EasingColorKeyFrame KeyTime="0"
    11. Value="{StaticResource ControlPressedColor}" />
    12. </ColorAnimationUsingKeyFrames>
    13. </Storyboard>
    14. </VisualState>
    15. <VisualState x:Name="Disabled">
    16. <Storyboard>
    17. <ColorAnimationUsingKeyFrames Storyboard.TargetName="Arrow"
    18. Storyboard.TargetProperty="(Shape.Fill).
    19. (SolidColorBrush.Color)">
    20. <EasingColorKeyFrame KeyTime="0"
    21. Value="{StaticResource DisabledForegroundColor}" />
    22. </ColorAnimationUsingKeyFrames>
    23. </Storyboard>
    24. </VisualState>
    25. </VisualStateGroup>
    26. ...


    Es war gar nicht so leicht, das auf Anhieb zu verstehen - noch, da die Fehlermeldung nicht mal signalisiert, was eigentlich das Problem ist oder verursacht. Und den krassen Effekt auf ja eigentlich "nur" eine Background-Eigenschaft hatte ich so auch nicht erwartet. Oh man :D