将控件绑定到不同的DataContexts(Binding controls to different DataContexts)

编程入门 行业动态 更新时间:2024-10-06 10:34:30
将控件绑定到不同的DataContexts(Binding controls to different DataContexts)

我有一种情况,我想将ListBox与类items列表和SolidColorBrush属性绑定到作为ListBox本身一部分的TextBlock foreground 。

ListBox数据来自MyColors类的User和SolidColorBrush属性但是,我无法同时为它们设置DataContexts 。 设置DataContext两次会覆盖第一个,并且不会填充ListBox 。 请帮忙!

public Page1() { this.DataContext = GetUsers(); this.DataContext = textcolor; // <-overrides the previous DataContext }

代码背后:

XAML:

<Grid HorizontalAlignment="Left" Height="650" Margin="0,38,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="480"> <ListBox x:Name="lstBani1" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock x:Name="tb1" Text="{Binding string1}" Foreground="{Binding Path=Brush1, Mode=OneWay}" Width="480"/> <TextBlock x:Name="tb2" Text="{Binding string2}" Width="480"/> <TextBlock x:Name="tb3" Text="{Binding string3}" Width="480"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>

CS:

public partial class Page1 : PhoneApplicationPage { public Page1() { InitializeComponent(); MyColors textcolor = new MyColors(); textcolor.Brush1 = new SolidColorBrush(Colors.Red); this.DataContext = GetUsers(); this.DataContext = textcolor; // <-overrides the previous DataContext } private List<User> GetUsers() {...} } public class User { public string string1 { get; set; } public string string2 { get; set; } public string string3 { get; set; } } public class MyColors : INotifyPropertyChanged { private SolidColorBrush _Brush1; public event PropertyChangedEventHandler PropertyChanged; public SolidColorBrush Brush1 { get { return _Brush1; } set { _Brush1 = value; NotifyPropertyChanged("Brush1"); } } public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

I have a situation where I want to bind a ListBox with a List of class items and SolidColorBrush property to foreground of a TextBlock that is part of the ListBox itself.

Data of ListBox comes from the class User and SolidColorBrush property from the class MyColors However, I am not able to set DataContexts for both of them at the same time. Setting DataContext twice overrides the first one and the ListBox is not populated. Please help!

public Page1() { this.DataContext = GetUsers(); this.DataContext = textcolor; // <-overrides the previous DataContext }

Code Behind:

xaml:

<Grid HorizontalAlignment="Left" Height="650" Margin="0,38,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="480"> <ListBox x:Name="lstBani1" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock x:Name="tb1" Text="{Binding string1}" Foreground="{Binding Path=Brush1, Mode=OneWay}" Width="480"/> <TextBlock x:Name="tb2" Text="{Binding string2}" Width="480"/> <TextBlock x:Name="tb3" Text="{Binding string3}" Width="480"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>

cs:

public partial class Page1 : PhoneApplicationPage { public Page1() { InitializeComponent(); MyColors textcolor = new MyColors(); textcolor.Brush1 = new SolidColorBrush(Colors.Red); this.DataContext = GetUsers(); this.DataContext = textcolor; // <-overrides the previous DataContext } private List<User> GetUsers() {...} } public class User { public string string1 { get; set; } public string string2 { get; set; } public string string3 { get; set; } } public class MyColors : INotifyPropertyChanged { private SolidColorBrush _Brush1; public event PropertyChangedEventHandler PropertyChanged; public SolidColorBrush Brush1 { get { return _Brush1; } set { _Brush1 = value; NotifyPropertyChanged("Brush1"); } } public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

最满意答案

有很多方法可以实现这一目标。

虽然今晚我在桌子上的时间有限,但我会以一种凌乱的ViewModel方式进行。

而不是试图将两个对象传递到datacontext中。 我建议的是创建通常称为视图模型的东西。 如果你不熟悉MVVM模式,可以在线阅读相当多的内容。 然而,这是一个很大的模式,我只在其大部分工作中使用它的一小部分。

根据您上面的代码,我添加了一个类似于以下内容的类。

public class AViewModel { public List<User> Users { get; set; } public MyColors Colours { get; set; } }

这是我将作为数据上下文传递的对象,并在将其传递给datacontext之前新建了Users和MyColors列表。

现在,对于列表框我可以做这样的事情......

<ListBox ItemsSource="{Binding Users}" Foreground="{Binding Colours.Brush1}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock x:Name="tb1" Text="{Binding string1}" Foreground="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}" Width="480"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

我将Listbox的前景绑定到颜色,然后使用TextBlocks的Foreground DP上的相对源传递它

我使用Windows UNI应用程序执行此操作,并且在项目中显示红色文本。 但是,有很多东西可以覆盖这种颜色。 我们可以整晚谈论这件事。

希望这对你有所帮助。

there are a quite a few ways to achieve this.

Though as my time at the desk here is limited tonight I'll do it in a bit of a messy ViewModel type way.

Rather than trying to pass two objects into the datacontext. What I would suggest is creating what is commonly called a View Model. If you aren't familiar with the MVVM pattern, there's quite a lot on-line to read up on. It's a large pattern however and I only use small parts of it for most of my work.

Based on your code from above, I have added a class which looks something like the following.

public class AViewModel { public List<User> Users { get; set; } public MyColors Colours { get; set; } }

This is the object I'm going to pass as the data context, and newing up the list of Users and MyColors before passing it to the datacontext.

Now, for the list box I can do something like this...

<ListBox ItemsSource="{Binding Users}" Foreground="{Binding Colours.Brush1}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock x:Name="tb1" Text="{Binding string1}" Foreground="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}" Width="480"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

I bind the foreground of the Listbox to the colour, and then pass that using relative source on the TextBlocks's Foreground DP

I did this with a Windows UNI app and I have red text showing in my items. However, there are numerous things which could override this colour. And we could go on all night talking about that.

Hope this is of some help to you.

更多推荐

本文发布于:2023-07-17 10:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   控件   DataContexts   Binding   controls

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!