将控件绑定到不同的DataContexts

时间:2015-07-04 19:13:50

标签: c# wpf xaml data-binding listbox

我的情况是,我希望将ListBoxitemsSolidColorBrush属性的列表绑定到foreground的{​​{1}} TextBlock本身的一部分。

ListBox的数据来自班级ListBox的班级UserSolidColorBrush属性但是,我无法为MyColors设置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));
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

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

虽然我今晚在这里的时间有限,但我会以一种凌乱的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应用程序执行此操作,并且在我的项目中显示红色文本。但是,有很多东西可以覆盖这种颜色。我们可以整晚谈论这件事。

希望这对你有所帮助。