简体   繁体   中英

Obtaining data from ControlTemplate/DataTemplate

I'm looking for advice, my problem is obtaining data(written text by user) from TextBox which is allocated by ControlTemplate in RadioButton. RadioButton is created programmatically in ScrollViewer.

XAML:

<UserControl.Resources>
    <ControlTemplate x:Name="rbTemplate" TargetType="RadioButton">
        <RadioButton>   
            <TextBox></TextBox>
        </RadioButton>
    </ControlTemplate> 
</UserControl.Resources>

Thanks for any help !

As @XAMlMAX has correctly mentioned, in WPF we use data binding to deal with obtaining data from the UI. You can find out the full story in the Data Binding Overview page on MSDN. However, in short, you need to add a property to receive the data and then data bind it to the TextBox.Text property:

<UserControl.Resources>
    <ControlTemplate x:Name="rbTemplate" TargetType="RadioButton">
        <RadioButton>   
            <TextBox Text="{Binding YourStringProperty}" />
        </RadioButton>
    </ControlTemplate> 
</UserControl.Resources>

Then in your code behind or view model, you can obtain the entered text from the YourStringProperty property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM