简体   繁体   English

将ListBox内部的控件绑定到ListBox的源以外的其他源

[英]Binding controls inside ListBox to a source other than the source of ListBox

I have a ListBox bound to a source which provides data to the text property of the controls inside. 我有一个绑定到源的ListBox ,该源向内部控件的text属性提供数据。 Now I'd like to bind Foreground property of my textboxes to a different source other than the one the main ListBox is bound to! 现在,我想将文本框的Foreground属性绑定到与主ListBox绑定的源不同的其他源!

My listbox is bound to a ObservableCollection and I want my textblock Foreground property bound to textColor which is located in ViewModel 我的列表框绑定到一个ObservableCollection,我希望将我的textblock前景属性绑定到ViewModel中的textColor上

public SolidColorBrush textColor
{
    get { return new SolidColorBrush(Colors.Red); }
}

both are in ViewModel class. 两者都在ViewModel类中。 I tried using Foreground="{Binding textColor}" but it seems XAML doesn't see it at all, should I do anything in the page so that it can see it, or is it because the parent ( ListBox ) is using a different source? 我尝试使用Foreground="{Binding textColor}"但似乎XAML根本看不到它,应该在页面中执行任何操作以便它可以看到它,还是因为父级( ListBox )使用了不同的名称?资源?

Edit : 编辑:

More details: 更多细节:

I have a DataContext.cs class which I have defined my tables in it. 我有一个DataContext.cs类,已在其中定义了表。 I have a ViewModel.cs class which I have these in it 我有一个ViewModel.cs类,我在其中

public class CViewModel : INotifyPropertyChanged
{
    private CDataContext myDB;

    public CViewModel(string DBConnectionString)
    {
        myDB = new CDataContext(DBConnectionString);
    }

    private ObservableCollection<Groups> _allGroups;
    public ObservableCollection<Groups> AllGroups
    {
        get { return _allGroups; }
        set
        {
            _allGroups = value;
            NotifyPropertyChanged("AllGroups");
        }
    }

    public string textColor
    {
        get { return "Tomato"; }
    }
}

Then I have my XAML file MainPage.xaml : 然后,我有了XAML文件MainPage.xaml

....
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox Margin="0,8,0,0"  toolkit:TiltEffect.IsTiltEnabled="True" x:Name="list" ItemsSource="{Binding AllGroups}" HorizontalAlignment="Center"  BorderThickness="4">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="Orange" Width="125" Height="125" Margin="6" Tap="Counterlist_OnTap">
                    <TextBlock Name="gname" Foreground="White" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/>
                    <TextBlock Name="ccl" Margin="0,0,0,-5" Foreground="{Binding textColor}" Text="{Binding Count}" FontSize="26" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
....

I also set DataContext of my MainPage to ViewModel in code behind: 我还在后面的代码中将MainPage DataContext设置为ViewModel

this.DataContext = App.ViewModel;

All you have to do is declare a static class (eg a singleton with per-instance access) and explicitly set the property binding to look in that class instead of the parent-bound model. 您要做的就是声明一个静态类(例如,具有按实例访问权限的单例),并显式设置属性绑定以在该类中查找,而不是绑定到父绑定模型。

Bottom line: Just set the Source explicitly through a StaticResource . 底线:只需通过StaticResource显式设置Source

The textColor property is part of the CViewModel , not the Groups object that is the datacontext within the ItemTemplate. textColor属性是CViewModel一部分,而不是ItemTemplate中属于datacontext的Groups对象。

Within the ItemTemplate you can reach out to the parent ViewModel with the following Element binding: 在ItemTemplate内,您可以通过以下元素绑定来访问父ViewModel:

<TextBlock Name="ccl" Margin="0,0,0,-5"
           Foreground="{Binding ElementName=ContentPanel, Path=DataContext.textColor}"
           Text="{Binding Count}" FontSize="26"
           VerticalAlignment="Bottom" HorizontalAlignment="Left" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM