C# 将多个属性绑定到不同的源
我的装订: 祖先的数据上下文:C# 将多个属性绑定到不同的源,c#,wpf,data-binding,C#,Wpf,Data Binding,我的装订: 祖先的数据上下文: detailsPanel.DataContext = client <DataTemplate x:Key="PlayersItemsControlTemplate" > <Button Content="{Binding Name}" IsEnabled="{Binding InConversation,Converter={StaticResource MyStatusToButtonEnableConv
detailsPanel.DataContext = client
<DataTemplate x:Key="PlayersItemsControlTemplate" >
<Button Content="{Binding Name}" IsEnabled="{Binding InConversation,Converter={StaticResource MyStatusToButtonEnableConverter}}"> </Button>
</DataTemplate>
项目控件:
<ItemsControl
x:Name="PlayersItemsControl"
ItemsSource="{Binding Peers}"
ItemsPanel="{StaticResource PlayersItemsControlPanel}"
ItemTemplate="{StaticResource PlayersItemsControlTemplate}">
</ItemsControl>
项目模板
detailsPanel.DataContext = client
<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"));
}
}
}
公共类客户端:INotifyPropertyChanged
{
//itemscontrol项源
私人可观察收集对等点;
公共可观测收集对等点
{
得到
{
if(peers==null)
对等方=新的ObservableCollection();
回报同行;
}
}
//我不想绑定到按钮的IsEnabled属性的属性
私人纠纷;
公开辩论
{
获取{return inConversation;}
设置
{
不可转换=价值;
if(PropertyChanged!=null)
财产变更(即新的财产变更通知(“变更通知”);
}
}
}
这些项绑定到Peers集合,每个textblock绑定到当前Peer的名称
我遇到的问题是,我需要将每个按钮(项)绑定到客户端中的不同属性
除了集合中的当前对等项之外,还有“InConversation”
这样的绑定怎么做?有多种解决方案,一种是使用绑定:
当然,只有在ItemsControl
中使用DataTemplate
时,它才会起作用
一种更好的方法是使用所谓的DataContextSpy
(and)直接绑定到另一个控件的DataContext