C# ListView中包含的UserControl中的绑定问题
我的应用程序结构如下: MainPage.xaml有一个C# ListView中包含的UserControl中的绑定问题,c#,xaml,data-binding,datacontext,win-universal-app,C#,Xaml,Data Binding,Datacontext,Win Universal App,我的应用程序结构如下: MainPage.xaml有一个ListView,它的ItemSource设置为CollectionViewSource,在代码隐藏中填充: MainPage.xaml 其中,groups是一个Linq查询,它通过对象属性对对象进行分组 要在ListView中成功显示my groups对象,这一切都很正常。我遇到的问题是在单个列表项的布局中。该模板如下所示,包括在另一个文件中定义的Usercontrol MainPage.xaml 我的绑定不起作用,我不知道如何让它们起作
ListView
,它的ItemSource
设置为CollectionViewSource
,在代码隐藏中填充:
MainPage.xaml
其中,groups
是一个Linq
查询,它通过对象属性对对象进行分组
要在ListView中成功显示my groups对象,这一切都很正常。我遇到的问题是在单个列表项的布局中。该模板如下所示,包括在另一个文件中定义的Usercontrol
MainPage.xaml
我的绑定不起作用,我不知道如何让它们起作用。目前,MinutesOverlay控件的可见性由一个绑定控制,只要我不设置MinutesOverlay的Datacontext,该绑定就可以工作。如果我通过执行this.Datacontext=this
来设置它,那么绑定没有效果,覆盖始终可见(大部分时间应该是折叠的)
如果我在MainPage.xaml中设置了过期和未绑定警报的值,效果很好。您是否尝试在您的userControl上设置名称(x:name=“userControl”)并将绑定更改为Text=“{binding Path=Alert,ElementName=userControl}”您尝试过设置名称(x:name=“userControl”)吗在您的usercontrol上,将绑定更改为Text=“{binding Path=Alert,ElementName=usercontrol}”?这就成功了,您能解释一下原因吗?此外,如果你把它作为一个答案添加,我可以接受它作为一个答案添加。MSDN:“默认情况下,绑定继承DataContext属性指定的数据上下文(如果已设置)。但是,ElementName属性是可以显式设置绑定源并重写继承的数据上下文的方法之一。”-
<Page.Resources>
...
<CollectionViewSource x:Key="src" IsSourceGrouped="True" />
...
</Page.Resources>
<Grid>
...
<ListView
ItemsSource="{Binding Source={StaticResource src}}"
SelectionMode="None"
ItemTemplate="{StaticResource processTemplate}"
ItemContainerStyle="{StaticResource ListViewItemStyle}">
<ListView.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource groupTemplate}"/>
</ListView.GroupStyle>
</ListView>
...
</Grid>
var cvs = (CollectionViewSource)Resources["src"];
cvs.Source = groups.ToList();
<DataTemplate x:Name="processTemplate">
<Grid>
...
<TextBlock Text="{Binding Path=Process}" ... />
<TextBlock Text="{Binding Path=Description}" ... />
<TextBlock Text="{Binding Path=LastSuccess}" ... />
<Button Grid.Column="1" Grid.RowSpan="3"
Background="{Binding Path=Status,
Converter={StaticResource stbConverter}}" ... />
<local:MinutesOverlay ... Visibility="{Binding Path=Status,
Converter={StaticResource stoConverter}}"
Overdue="{Binding Path=MinutesWarning}"
Alert="{Binding Path=MinutesAlert}"/>
</Grid>
</DataTemplate>
<Grid>
...
<TextBlock Text="{Binding Path=Overdue}" />
<TextBlock Text="{Binding Path=Alert}" />
...
</Grid>
public sealed partial class MinutesOverlay : UserControl
{
public MinutesOverlay()
{
this.InitializeComponent();
}
public static readonly DependencyProperty OverdueProperty = DependencyProperty.Register(
"Overdue", typeof(int), typeof(MinutesOverlay), new PropertyMetadata(0));
public static readonly DependencyProperty AlertProperty = DependencyProperty.Register(
"Alert", typeof(int), typeof(MinutesOverlay), new PropertyMetadata(0));
public int Overdue
{
get { return (int)GetValue(OverdueProperty); }
set { SetValue(OverdueProperty, value); }
}
public int Alert
{
get { return (int)GetValue(AlertProperty); }
set { SetValue(AlertProperty, value); }
}
}