Bind Dependency Property, defined in Code-Behind, through Xaml to a Property in the DataContext of a UserControl

前端 未结 1 1117
礼貌的吻别
礼貌的吻别 2021-01-25 06:20

I want to use code similar to that found here. The issue I\'m having is that I\'d like to extend it to allow the value set in the XAML to use {Binding PropertyOfViewModel}

相关标签:
1条回答
  • 2021-01-25 07:03

    If I understood everything correctly then what you need is a standard MVVM scenario and standard bindings.

    It can be done either like this:

    <local:TestControl TestObject="{Binding PropertyOfViewModel}">
    </local:TestControl>
    

    Or like this:

    <local:TestControl>
      <local:TestControl.TestObject>
        <Binding Path="PropertyOfViewModel"/>
      </local:TestControl.TestObject>
    </local:TestControl>
    

    Update: In response to the code of your UserControl you've shown...

    What you are doing is placing a control inside itself, which obviously will give you a StackOverflow exception. You don't need to define a ContentTemplate inside UserControl. You can just place the content directly as a child element:

    <UserControl 
        x:Class="Mage.Views.AddClientView"
        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" 
        xmlns:Views="clr-namespace:Mage.Views"
        Background="{StaticResource WindowBackgroundBrush}"
        mc:Ignorable="d"  d:DesignWidth="320" d:DesignHeight="145"
        x:Name="AddClientV"
        MaxHeight="145" MinHeight="145">
    
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState x:Name="Show">
                    <VisualState.Storyboard>
                        <Storyboard>
                        <DoubleAnimation 
                                Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleY)" 
                                Storyboard.TargetName="AddClientV" 
                                From="0" 
                                To="1" 
                                Duration="0:0:1" />
                        </Storyboard>
                    </VisualState.Storyboard>
                </VisualState>
    
                <VisualState x:Name="Hide">
                    <VisualState.Storyboard>
                        <Storyboard>
                        <DoubleAnimation 
                            Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleY)" 
                            Storyboard.TargetName="AddClientV" 
                            From="1" 
                            To="0" 
                            Duration="0:0:1" />
                        </Storyboard>
                    </VisualState.Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    
        <Views:AddClientView.RenderTransform>
                <ScaleTransform ScaleY="1" />
        </Views:AddClientView.RenderTransform>
        <Views:AddClientView.IsInAcceptDataMode>
                <Binding Path="IsInInputMode"/>
        </Views:AddClientView.IsInAcceptDataMode>
    
       <Grid>
          <Label 
            Height="25" 
            Width="306"
            HorizontalAlignment="Left" 
            Margin="12,12,0,0" 
            OverridesDefaultStyle="False" 
            Style="{DynamicResource CalloutLabel}" 
            VerticalAlignment="Top" Content="Company Name (Name of Organizational Unit)"/>
    
        <TextBox Height="23" Margin="12,41,12,0" VerticalAlignment="Top" TabIndex="1" />
    
        <Button 
            Style="{DynamicResource LightButton}" 
            Height="23" Width="75" 
            HorizontalAlignment="Right" 
            VerticalAlignment="Bottom" 
            Margin="0,0,97,12" 
            TabIndex="4">OK</Button>
    
        <Button 
            Style="{DynamicResource LightButton}" 
            Height="23" Width="75" 
            HorizontalAlignment="Right" 
            VerticalAlignment="Bottom" 
            Margin="0,0,12,12" 
            Command="{Binding Cancel}"
            TabIndex="3">Cancel</Button>
    
        <CheckBox Content="Make Groups" Height="16" IsChecked="True" FlowDirection="RightToLeft" Margin="150,79,12,0" VerticalAlignment="Top" TabIndex="2" />
    
        <Rectangle Fill="{DynamicResource HeaderBarFill}" Height="5" Margin="0,0,0,0" Stroke="{x:Null}" VerticalAlignment="Bottom" />
      </Grid>
    </UserControl>
    
    0 讨论(0)
提交回复
热议问题