C# 使用XAML绑定依赖项属性
在用户控件MainControl中,我有一个名为Repository的依赖属性。我想在MainWindow中使用xaml绑定此存储库属性C# 使用XAML绑定依赖项属性,c#,.net,wpf,C#,.net,Wpf,在用户控件MainControl中,我有一个名为Repository的依赖属性。我想在MainWindow中使用xaml绑定此存储库属性 <my:MainControl x:Name="mainControl" Repository="{Binding }" Visibility="Visible" /> 在MainControl构造函数中,我想使用 public MainControl() { InitializeComponent(); lblCountCars
<my:MainControl x:Name="mainControl" Repository="{Binding }" Visibility="Visible" />
在MainControl构造函数中,我想使用
public MainControl()
{
InitializeComponent();
lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars());
}
我在这里做错了什么吗Repository=“{Binding}”
?当我在代码中注册用户控件时,一切都正常,但我想学习如何使用xaml来实现这一点
更新
说清楚。我有main窗口
,它使用两个用户控件<代码>主控制和treasurecontrol
。
我想将类型为ICarRepository
的存储库发送到此控件中的任何一个,因此我在MainControl
和treasurecontrol
中创建了类型为ICarRepository
的名为repository
的DependencyProperty
我的目标是将存储库实例发送到MainControl
repository
property(DP),并在标签上打印内容属性lblCountCars.Content=repository.CountAllCars()代码>
我还希望用户控件中的这个存储库实例能够进一步工作,而不仅仅是显示简单的文本
所以我试着用下面的建议
MainWindow.xaml
<my:MainControl x:Name="mainControl" Repository="{Binding Repository}" />
MainControl.xaml
<UserControl x:Name="mainControl">
<Label Name="lblCountBooks" Content="{Binding ElementName=mainControl, Path=Repository.CountAllBooks()}"
<ItemsControl ItemsSource="{Binding ElementName=mainControl, Path=Repository}" />
标签内容未使用预期内容更新。在usercontrols中处理依赖项属性时,我使用elementname绑定绑定到DP。所以我会移除你的
lblCountCars.Content=string.Format(“有{0}辆车。”,
Repository.CountAllCars())
这样做:
<UserControl x:Name="uc">
...
<Label Content="{Binding ElementName=uc, Path=Repository.Count}"/><!-- you can use ContentStringFormat here to get you formattet string-->
<ItemsControl ItemsSource="{Binding ElementName=uc, Path=Reportsitory}"/>
...
主窗口中的绑定看起来不错
编辑:
您可以只绑定到属性。因此,您需要一个属性,而不是Repository.CountAllBooks()。如果您没有机会在ICarRepository上创建属性,可以使用converter和Repository属性作为convertparameter来获取您的信息。在usercontrols中处理依赖项属性时,我使用elementname绑定到DP。所以我会移除你的
lblCountCars.Content=string.Format(“有{0}辆车。”,
Repository.CountAllCars())
这样做:
<UserControl x:Name="uc">
...
<Label Content="{Binding ElementName=uc, Path=Repository.Count}"/><!-- you can use ContentStringFormat here to get you formattet string-->
<ItemsControl ItemsSource="{Binding ElementName=uc, Path=Reportsitory}"/>
...
主窗口中的绑定看起来不错
编辑:
您可以只绑定到属性。因此,您需要一个属性,而不是Repository.CountAllBooks()。如果您没有机会在ICarRepository上创建属性,可以使用converter和Repository属性作为convertparameter来获取您的信息。在usercontrols中处理依赖项属性时,我使用elementname绑定到DP。所以我会移除你的
lblCountCars.Content=string.Format(“有{0}辆车。”,
Repository.CountAllCars())
这样做:
<UserControl x:Name="uc">
...
<Label Content="{Binding ElementName=uc, Path=Repository.Count}"/><!-- you can use ContentStringFormat here to get you formattet string-->
<ItemsControl ItemsSource="{Binding ElementName=uc, Path=Reportsitory}"/>
...
主窗口中的绑定看起来不错
编辑:
您可以只绑定到属性。因此,您需要一个属性,而不是Repository.CountAllBooks()。如果您没有机会在ICarRepository上创建属性,可以使用converter和Repository属性作为convertparameter来获取您的信息。在usercontrols中处理依赖项属性时,我使用elementname绑定到DP。所以我会移除你的
lblCountCars.Content=string.Format(“有{0}辆车。”,
Repository.CountAllCars())
这样做:
<UserControl x:Name="uc">
...
<Label Content="{Binding ElementName=uc, Path=Repository.Count}"/><!-- you can use ContentStringFormat here to get you formattet string-->
<ItemsControl ItemsSource="{Binding ElementName=uc, Path=Reportsitory}"/>
...
主窗口中的绑定看起来不错
编辑:
您可以只绑定到属性。因此,您需要一个属性,而不是Repository.CountAllBooks()。如果您没有机会在ICarRepository上创建属性,可以使用converter和Repository属性作为convertparameter来获取信息。这可能是因为您的MainControls数据上下文未在构造函数中设置。您需要使用DataContextChanged事件()
例如:
public MainControl()
{
InitializeComponent();
DataContextChanged += MainControl_DataContextChanged;
}
void MainControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars());
}
这可能是因为未在构造函数中设置MainControls数据上下文。您需要使用DataContextChanged事件()
例如:
public MainControl()
{
InitializeComponent();
DataContextChanged += MainControl_DataContextChanged;
}
void MainControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars());
}
这可能是因为未在构造函数中设置MainControls数据上下文。您需要使用DataContextChanged事件()
例如:
public MainControl()
{
InitializeComponent();
DataContextChanged += MainControl_DataContextChanged;
}
void MainControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars());
}
这可能是因为未在构造函数中设置MainControls数据上下文。您需要使用DataContextChanged事件()
例如:
public MainControl()
{
InitializeComponent();
DataContextChanged += MainControl_DataContextChanged;
}
void MainControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars());
}
请为您的主控发布您的相关xaml,可能为您的DP too发布您的相关xaml,可能为您的DP too发布您的相关xaml,可能为您的主控发布您的相关xaml,可能为您的DP too发布您的相关xaml,也可能为您的DP发布代码