locked
How can I binding canvas to an observablecollection WPF RRS feed

  • Question

  • I created a Scroll bar at the bottom of the window,

    to scroll a simple text written manually, it works well

    But I want to scroll a list of items from my database, I do the binding to an ObservableCollection.. but I don't have the return of my ItemSource... I have the return :

    (Collection) 

    in the Scroll bar !

    The XAML:

    <Window x:Class="WPFAuthentification.Views.NewUserView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
               Title="NewUserView" Height="500" Width="800" Icon="../Images/fuelSoft.png">
    		 <Grid Background="White" VerticalAlignment="Top" HorizontalAlignment="Center">   
    		   //....
    		   
    		    <Canvas Background="White" Margin="-717,-281,151,63" Grid.ColumnSpan="4" >
                   <Canvas Canvas.Top="572" ClipToBounds="True" Name="canMain" Background="LightGray" 
    			     Canvas.Left="614" Loaded="canMain_Loaded"  >
                       <TextBlock  Text="{Binding RoleList}"  FontSize="25" Name="tbmarquee" Canvas.Top="25" Width="1346" 
                                 RenderTransformOrigin="0.465,0.524" />
                    
                    </Canvas>         
               
               </Canvas>      
            
             </Grid>
        </Window>
     

    the XAML.cs:

    public NewUserView()
           {
               InitializeComponent();
           }
    
                private void canMain_Loaded(object sender, RoutedEventArgs e)
           {
               DoubleAnimation doubleAnimation = new DoubleAnimation();
               doubleAnimation.From = -tbmarquee.ActualWidth;
               doubleAnimation.To = canMain.ActualWidth;
               doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
               doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:18"));
               tbmarquee.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
           }

    The ViewModel:

     public NewUserViewModel(int userId)
            {
    		    //....
    		}
                if
             private ObservableCollection<UserRoleClass> roleList;
             public ObservableCollection<UserRoleClass> RoleList
              {
                get { return roleList; }
                set
                {
                    roleList = value;
                    OnPropertyChanged("RoleList");
                }
              }

    the result:

    Monday, July 10, 2017 3:27 PM

All replies

  • I am really confused reading your post. 

    I see no ScrollBar in your XAML???

    Now what I guess is that you want to show one item from an ItemsSource.  If you want to do this (and this is a real guess) is have a collection of items which you show one at a time with a ScrollBar to scroll each item (showing one at a time).



    Lloyd Sheen

    Monday, July 10, 2017 6:44 PM
  •  I am sorry if my post is not clear.

    In fact,Yes that's it, I have a collection of items that I put them both with a ScrollBar to scroll it, provided by showing one or more than one at a time (5 or 10 at a time)

    In fact, I want objects from a collection to be scroll it in a Canvas (or if there are other suggestions better than Canvas), like the Scrollbar of news in CNN or SkyNews ...

    I have tried a simple Text (not item from a collection) , and it scroll good in a Canvas :

    the XAML:

    	<Canvas Background="White" Margin="-715.19,-218.354,-370.253,0" >
                   <Canvas Canvas.Top="567" ClipToBounds="True" Name="canMain" Background="LightGray"   Height="97" Width="1340"
    			        Canvas.Left="702" Loaded="canMain_Loaded"  >
    			
                            <TextBlock   FontSize="25" Name="tbmarquee" Canvas.Top="25" Width="1346" 
    				             RenderTransformOrigin="0.465,0.524">Test Scroll Item like CNN...
    	                </TextBlock>
                  </Canvas>
            </Canvas>

    the XAML.cs:

     private void canMain_Loaded(object sender, RoutedEventArgs e)
            {
                DoubleAnimation doubleAnimation = new DoubleAnimation();
                doubleAnimation.From = -tbmarquee.ActualWidth;
                doubleAnimation.To = canMain.ActualWidth;
                doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
                doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:18"));
                tbmarquee.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
            }

    For a writing manually text is Ok it is scrolling , but for items from a collection , I don't know how to do it? Any suggestion?

    Thanks,

    Tuesday, July 11, 2017 9:23 AM
  • Hi,

    >> In fact, I want objects from a collection to be scroll it in a Canvas

    I see you are using DoubleAnimation  to create an animation for the textblock to scroll, not ScrollBar .

    >> like the Scrollbar of news in CNN or SkyNews

    Would you show the screenshots of what you want directly for us ?

    >>I have a collection of items that I put them both with a ScrollBar to scroll it, provided by showing one or more than one at a time (5 or 10 at a time)

    What elements do you use? listBox,ListView, or userControl?

    After my searching, maybe below discussions would be helpful.

    wpf auto scroll items in databound ItemsControl (like news ticker)

    Wpf c# ticker rss news reader

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 17, 2017 9:20 AM