[英]Can I set my user control datacontext in xaml via a dependency property bound to a property on the parent control in wpf?
[英]WPF Custom control property set via XAML in own tag
我正在構建一個WPF控件,它將在“顯示”模式下顯示一個組件,然后單擊一次,將切換到“編輯”模式下的組件。
將綁定這兩個組件,以使編輯值反映在顯示組件(通常是TextBlock)上。
我期望的XAML語法如下。
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Rating Year">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Controls:EditableControlContainer>
<Controls:EditableControlContainer.DisplayControl>
<TextBlock Text="{Binding ValueString}" />
</Controls:EditableControlContainer.DisplayControl>
</Controls:EditableControlContainer>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView.View>
背后的代碼
public partial class EditableControlContainer : UserControl
{
private UserControl displayControl;
public UserControl DisplayControl {
get { return displayControl; }
set { displayControl = value; }
}
private UserControl editControl;
public UserControl EditControl {
get { return editControl; }
set { editControl = value; }
}
public EditableControlContainer() {
InitializeComponent();
}
}
我收到的錯誤是EditableControlContainer.DisplayControl無法識別或無法訪問。
我也不確定是否可以包含一個對象或鍵入UserControl(最好是我想使用一種接口,盡管我不確定是哪一種)。
任何人都可以解釋或提供指向資源的鏈接,該資源演示如何在XAML中配置UserControl中的屬性嗎?
我瀏覽了該論壇,但找不到任何涵蓋此內容的內容。
非常感謝。
TextBlock不是UserControl,如果要在其中放置TextBlock,則需要將顯示控件的類型更改為TextBlock。
private TextBlock displayControl;
public TextBlock DisplayControl {
get { return displayControl; }
set { displayControl = value; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.