0

I need to get a specific ScrollViewer control from an XAML file in C#, so that I can try to implement a drag-and-scroll system where when an item is dragged near the edge it will scroll accordingly. But I need access to all the methods to do this, yet I cannot find any info as to how to get the specific scrollviewer from the XAML.

I need access to it from a separate .cs file in which most of the project's converters and functions are located. Not sure bout the downvotes tho seeing as I did lots of research on it. Here is some of the code:

<ScrollViewer  x:Name="timelineScrollerRight" Grid.Row="1" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Disabled" PreviewMouseWheel="TimelineScrollerRightMouseWheel">

That is the ScrollViewer tag that I want to access. I want to change the LineRight/LineLeft attributes of the ScrollViewer in a MouseMove event triggered by another control within the ScrollViewer.

The C# code is bare right now, the event is empty, I want to be able to declare a variable to allow me to change the ScrollViewer attributes so that when the MouseMove event is called, the ScrollViewer is scrolled and so on. The ScrollViewer is located in a file named TimelineAnimationView.xaml and the event happens in the code-behind, TimelineAnimationView.xaml.cs. But I would also like to access it from a separate .cs file if needed.

AliEgseem
  • 321
  • 4
  • 18

1 Answers1

3

Normally you access it using x:Name:

<ScrollViewer x:Name="scoller"/>

If you are doing this with loose XAML such as in a UserControl or Window, you just access it as a private instance variable scroller in your code-behind.

If you are doing this with a ScrollViewer in a template, then you have to use GetTemplateChild (in OnApplyTemplate) to find the child type with the relevant name:

scroller = GetTemplateChild("scroller");
NextInLine
  • 2,126
  • 14
  • 23
  • What would be the syntax to access a scrollviewer with `x:Name` in C# code-behind? – AliEgseem Mar 03 '15 at 20:09
  • You just use `scroller` as you would any field. So for instance, `scroller.GetHashCode()` would return the hash code, and `scroller.ActualWidth` returns the current width of the ScrollViewer. – NextInLine Mar 03 '15 at 20:13
  • Sorry, I meant how to initially get the xName scroller into a variable in the code-behind. How so I initialize it? – AliEgseem Mar 03 '15 at 20:15
  • If the control has a name attribute there should already be an automatically created reference to in a variable at the class level.. – Bradley Uffner Mar 03 '15 at 20:16
  • @AliEgseem I'm not sure I'm understanding your question. The `x:Name` goes in the XAML, and then it initializes a hidden field with the name `scroller` after the XAML is loaded via `InitializeComponent()`. Is that not working for you? – NextInLine Mar 03 '15 at 20:17
  • I was under the impression I could just do something like ScrollViewer.getByName(xname) or something like that. Is there no way to get a scrollviewer by xname from the code-behind? I am new to WPF and XAML so I don't know. – AliEgseem Mar 03 '15 at 20:26
  • Try reloading the answer - I made an edit and added more code. What kind of class are you doing this in? Is it a Control? Or a UserControl? A Window? Something else? – NextInLine Mar 03 '15 at 20:29
  • I'm doing this in a separate .cs file in which I have converters for bindings and such. I'm not sure if it is a class similar to the ones you mentioned, but I just want to access elements from the XAML from my .cs file. I'm used to PHP and such where you can get things by element names, is there anything similar? – AliEgseem Mar 03 '15 at 20:34
  • Without seeing the XAML and c# code you are working with, I'm not sure. – NextInLine Mar 03 '15 at 20:57
  • I added the ScrollViewer tag itself in the OP, but the C# code is empty aside from the event header. – AliEgseem Mar 03 '15 at 21:08
  • WPF normally works by having XAML inside of a class (the ` – NextInLine Mar 03 '15 at 21:29
  • The XAML is in ` – AliEgseem Mar 03 '15 at 21:38
  • Ok, so normally that XAML code would be in a `TimelineAnimationView.xaml`, with a `TimelineAnimationView.xaml.cs` file beneath it (assuming you are using Visual Studio). If you go into `TimelineAnimationView.xaml.cs` you should just be able to use the field `timelineScrollerRight` in your code, at least in non-static methods. – NextInLine Mar 03 '15 at 21:40
  • @NextInLine thank you! That is weird compared to other frameworks but I guess it is useful. Any way I could get access to timelineScrollerRight in files not near TimelineAnimationView? – AliEgseem Mar 03 '15 at 21:43
  • 1
    You can make a public property in `TimelineAnimationView` C# code that returns the value of the `timelineScrollerRight ` field. You then have to have access to the TimelineAnimationView object to access that property. – NextInLine Mar 03 '15 at 21:44