Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用XAML绑定依赖项属性_C#_.net_Wpf - Fatal编程技术网

C# 使用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

在用户控件MainControl中,我有一个名为Repository的依赖属性。我想在MainWindow中使用xaml绑定此存储库属性

<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发布代码