![](/img/trans.png)
[英]Data-Binding with Custom Dependency-Property of UserControl is not working
[英]Databinding partially working to custom dependency property in UserControl
哈羅,謝謝您的寶貴時間。
我有一個特殊的問題。 我創建了一個usercontol,它具有一些自定義的依賴項屬性。
如果我實現了usercontrol,並綁定到靜態文本。 一切正常。
但是,如果我嘗試將其設置為選定對象屬性的值。 這是行不通的。
這是我在輸出窗口中遇到的錯誤:
錯誤:BindingExpression路徑錯誤:在Helper.UserControls.UseCasePropertyDisplay上找不到“ SelectedUseCase”屬性。 BindingExpression:Path ='SelectedUseCase.Name'DataItem ='Helper.UserControls.UseCasePropertyDisplay'; 目標元素是'Helper.UserControls.UseCasePropertyDisplay'(Name ='null'); 目標屬性為“文本”(類型為“字符串”)
<UserControl
x:Class="Helper.UserControls.UseCasePropertyDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Helper.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel Orientation="Horizontal"
Margin="260,0,0,0">
<TextBlock Text="{Binding Label}"
Style="{StaticResource UseCaseTextBlock}"
x:Name="textblock_label"/>
<TextBlock x:Name="textblock_propertyContent"
Text="{Binding Text}"
Style="{StaticResource UseCaseFrameWorkTextElement}"
DoubleTapped="textblock_DoubleTapped" />
<TextBox x:Name="textbox_propertyContent"
Text="{Binding Text}"
Visibility="Collapsed"
Style="{StaticResource UseCaseTextBox}"
LostFocus="textbox_LostFocus" />
</StackPanel>
</UserControl>
代碼背后的依賴性屬性聲明:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/UserControls/DisplayAndEditControl.xaml.cs
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(UseCasePropertyDisplay),
new PropertyMetadata(null));
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
"Label",
typeof(string),
typeof(UseCasePropertyDisplay),
new PropertyMetadata(null));
public string Text
{
get { return (string)GetValue(TextProperty); }
set {SetValue(TextProperty, value);}
}
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value);}
}
這就是我在視圖中實現它的方式:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/ViewsAndViewModels/ViewUseCases.xaml
<uc:UseCasePropertyDisplay Label="Name" Text="{Binding SelectedUseCase.Name, Mode=TwoWay}" />
通過在這里閱讀非常相似的問題,我猜測這與我設置上下文的方式有關。 但是,已經提供給人們的解決方案(將相對來源設置為祖先)對我不起作用。 由於它在我的平台上不可用。 我不太確定從這里開始該怎么做,因為這是我第一次嘗試使用usercontrols,也是我第一次使用依賴項屬性。 直到幾周學校才開始上課,所以我不能因此而抓住我的老師。
而不是將DataContext全局設置,而是將第一個ui元素設置為繼承的DataContext: DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
如您所寫,您不能使用FindAncestor,因此可以嘗試使用名稱並對其進行引用:
<UserControl
x:Class="Helper.UserControls.UseCasePropertyDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Helper.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
x:Name="Root">
<StackPanel DataContext="{Binding ElementName=Root}"
Orientation="Horizontal"
Margin="260,0,0,0">
<TextBlock Text="{Binding Label}"
Style="{StaticResource UseCaseTextBlock}"
x:Name="textblock_label"/>
<TextBlock x:Name="textblock_propertyContent"
Text="{Binding Text}"
Style="{StaticResource UseCaseFrameWorkTextElement}"
DoubleTapped="textblock_DoubleTapped" />
<TextBox x:Name="textbox_propertyContent"
Text="{Binding Text}"
Visibility="Collapsed"
Style="{StaticResource UseCaseTextBox}"
LostFocus="textbox_LostFocus" />
</StackPanel>
</UserControl>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.