如何将canvas绑定到observablecollection WPF [英] How can I binding canvas to an observablecollection WPF

查看:110
本文介绍了如何将canvas绑定到observablecollection WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗口底部创建了一个滚动条,



滚动手动编写的简单文本,效果很好



但我想从我的数据库滚动一个项目列表,我绑定到一个ObservableCollection ..但我没有返回我的ItemSource ...我有返回:



(收藏)


滚动条中的




XAML:



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>





XAML.cs:





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);
       }





我尝试过:





What I have tried:

the ViewModel:

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

推荐答案

我不知道你的RoleList是如何定义的。



也许这个例子可以引导你走向正确的方向:

I do not know how your RoleList is defined.

Maybe this example can lead you into the right direction:
public class Role
{
   public string Name { get; set; }
   public string Type { get; set; }
}



你的ObservableCollection定义如下


And your ObservableCollection is defined like this

private ObservableCollection<Role> roleList;

public ObservableCollection<Role> RoleList
{
    get { return roleList; }
    set
    {
        if (roleList == value) return;

        roleList = value;

        OnPropertyChanged();
    }
}



然后你可以在ListBox中显示你的收藏。

你必须使用DataTemplate。


Then you could display your collection in a ListBox.
You must use a DataTemplate.

<ListBox ItemsSource="{Binding RoleList}">
	<ListBox.ItemTemplate>
	    <DataTemplate>
	        <Grid>
		    <Grid.ColumnDefinitions>
			<ColumnDefinition></ColumnDefinition>
			<ColumnDefinition></ColumnDefinition>
		    </Grid.ColumnDefinitions>
		    <TextBlock Text="{Binding Name}"></TextBlock>
		    <TextBlock Grid.Column="1" Text="{Binding Type}"></TextBlock>
		</Grid>
	    </DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>



单独的TextBlock无法显示集合。



更好想法是使用DataGrid。


A TextBlock alone cannot display a collection.

A better idea would be to use a DataGrid.

<DataGrid ItemsSource="{Binding RoleList}"
                      AutoGenerateColumns="False"
                      HeadersVisibility="Column"                 
                      CanUserAddRows="False">
	<DataGrid.Columns>
		<DataGridTemplateColumn>
			<DataGridTemplateColumn.HeaderTemplate>
				<DataTemplate>
					<TextBlock Text="Name"></TextBlock>
				</DataTemplate>
			</DataGridTemplateColumn.HeaderTemplate>
			<DataGridTemplateColumn.CellTemplate>
				<DataTemplate>
					<TextBlock Text="{Binding Name}"></TextBlock>
				</DataTemplate>
			</DataGridTemplateColumn.CellTemplate>
		</DataGridTemplateColumn>
		<DataGridTemplateColumn Width="*">
			<DataGridTemplateColumn.HeaderTemplate>
				<DataTemplate>
					<TextBlock Text="Type"></TextBlock>
				</DataTemplate>
			</DataGridTemplateColumn.HeaderTemplate>
			<DataGridTemplateColumn.CellTemplate>
				<DataTemplate>
					<TextBlock Text="{Binding Type}"></TextBlock>
				</DataTemplate>
			</DataGridTemplateColumn.CellTemplate>
		</DataGridTemplateColumn>
	</DataGrid.Columns>
</DataGrid>



如果你想要一个类似新闻自动收报机的东西你需要一个价值转换器

你的原件画布解决方案。


If you want to have something like a news ticker you need a value converter
for your original canvas solution.

namespace WpfApplicationTest1.Converters
{
    public class ListConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var roles = value as ObservableCollection<Role>;

            if (roles != null)
            {
                var stringBuilder = new StringBuilder();

                foreach (var role in roles)
                {
                    stringBuilder.Append(


{role.Name} {role.Type} );
}

返回stringBuilder.ToString();
}

返回null;
}

公共对象ConvertBack(对象值,类型targetType,对象参数,CultureInfo文化)
{
throw new NotImplementedException();
}
}
}
"{role.Name} {role.Type}"); } return stringBuilder.ToString(); } return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }



在您的视图中,您必须使用这样的值转换器。

将其添加到您的Window命名空间部分:


In your view you must use the value converter like this.
Add this to your Window namespace section:

xmlns:local="clr-namespace:WpfApplicationTest1"




<TextBlock  Text="{Binding RoleList, Converter={StaticResource ListConverter}}"  FontSize="25" Name="tbmarquee" Canvas.Top="25" Width="1346" 
                             RenderTransformOrigin="0.465,0.524" />


这篇关于如何将canvas绑定到observablecollection WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆