[英]WPF: Modifying a ControlTemplate property from a DataTemplate?
[英]Obtaining data from ControlTemplate/DataTemplate
我正在尋找建議,我的問題是從TextBox獲取數據(用戶編寫的文本),這是由RadioButton中的ControlTemplate分配的。 RadioButton是在ScrollViewer中以編程方式創建的。
XAML:
<UserControl.Resources>
<ControlTemplate x:Name="rbTemplate" TargetType="RadioButton">
<RadioButton>
<TextBox></TextBox>
</RadioButton>
</ControlTemplate>
</UserControl.Resources>
謝謝你的幫助 !
正如@XAMlMAX正確提到的,在WPF中我們使用數據綁定來處理從UI獲取數據。 您可以在MSDN上的“ 數據綁定概述”頁面中找到完整的故事。 但是,簡而言之,您需要添加一個屬性來接收數據,然后將數據綁定到TextBox.Text
屬性:
<UserControl.Resources>
<ControlTemplate x:Name="rbTemplate" TargetType="RadioButton">
<RadioButton>
<TextBox Text="{Binding YourStringProperty}" />
</RadioButton>
</ControlTemplate>
</UserControl.Resources>
然后在您的代碼隱藏或視圖模型中,您可以從YourStringProperty
屬性中獲取輸入的文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.