gpt4 book ai didi

c# - 将多个属性绑定(bind)到不同的来源

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:05 25 4
gpt4 key购买 nike

我的绑定(bind):

祖先的DataContext:

   detailsPanel.DataContext = client 

项目控制:

    <ItemsControl                                                                   
x:Name="PlayersItemsControl"
ItemsSource="{Binding Peers}"
ItemsPanel="{StaticResource PlayersItemsControlPanel}"
ItemTemplate="{StaticResource PlayersItemsControlTemplate}">
</ItemsControl>

项目模板

     <DataTemplate x:Key="PlayersItemsControlTemplate" >
<Button Content="{Binding Name}" IsEnabled="{Binding InConversation,Converter={StaticResource MyStatusToButtonEnableConverter}}"> </Button>
</DataTemplate>

项目来源:

        public class Client : INotifyPropertyChanged 
{
// the itemscontrol items source
private ObservableCollection<Player> peers;
public ObservableCollection<Player> Peers
{
get
{
if (peers == null)
peers = new ObservableCollection<Player>();
return peers;
}
}
// the property i wan't to bind to IsEnabled Property of the Button
private bool inConversation;
public bool InConversation
{
get {return inConversation; }
set
{
inConversation = value;
if(PropertyChanged != null)
PropertyChanged(this,new PropertyChangedEventArgs("InConversation"));
}
}
}

项目绑定(bind)到 Peers 集合,每个文本 block 绑定(bind)到当前 Peer 的名称。

我遇到的问题是我需要将每个按钮(项目)绑定(bind)到客户端中的不同属性“InConversation”除了集合中的当前 Peer 之外。

如何才能完成这样的绑定(bind)?

最佳答案

有多种解决方案,一种是使用RelativeSource绑定(bind):

<DataTemplate x:Key="PlayersItemsControlTemplate" >
<Button Content="{Binding Name}"
IsEnabled="{Binding Path=DataContext.InConversation,
RelativeSource={RelativeSource AncestorType=ItemsControl}}"></Button>
</DataTemplate>

当然,只有当您在 ItemsControl 中使用您的 DataTemplate 时,它才会起作用。

一个更好的方法是使用所谓的 DataContextSpy(infomore info)直接绑定(bind)到另一个控件的 DataContext

关于c# - 将多个属性绑定(bind)到不同的来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9542073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com