将控件绑定到不同的 DataContext

Binding controls to different DataContexts

我有一种情况,我想将 ListBox 与 class itemsSolidColorBrush 属性 的列表绑定到 foregroundTextBlockListBox 本身的一部分。

ListBox 的数据来自 class UserSolidColorBrush 属性 来自 class 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));
            }
        }
    }

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

虽然今晚我在办公桌前的时间有限,但我将以一种有点混乱的 ViewModel 类型的方式来完成。

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

根据您上面的代码,我添加了一个 class,如下所示。

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

这是我要作为数据上下文传递的对象,并在将其传递给数据上下文之前更新 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>

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

我使用 Windows UNI 应用程序完成了此操作,我的项目中显示了红色文本。但是,有很多东西可以覆盖这种颜色。我们可以通宵讨论那个。

希望对您有所帮助。