WPF user control bind data to user control property

前端 未结 1 391
我在风中等你
我在风中等你 2021-02-04 01:37

I have user control:

xaml



        
相关标签:
1条回答
  • 2021-02-04 01:49

    You can try Element binding inside the user control. Just give a name to UserControl and bind property:

    <UserControl x:Class="controlmaker.checkButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="114" d:DesignWidth="221"
             x:Name="MyUserControl">
    <Grid Background="Aqua" >
        <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left"
              Margin="58,24,0,0" Name="checkBox1" VerticalAlignment="Top" />
        <Button Content="{Binding Path=buttText, ElementName=MyUserControl}"
              Height="23" HorizontalAlignment="Left" Margin="58,57,0,0" 
              Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
    </UserControl>
    

    And then you can Bind or put static text from user control usage place

    <my:checkButton buttText="aka" />
    

    or

      <my:checkButton buttText="{Binding SomeProperty}" />
    
    0 讨论(0)
提交回复
热议问题