简体   繁体   中英

WPF Custom control property set via XAML in own tag

I'm building a WPF control which will display a component in Display mode, then on a click, will switch to a component in Edit mode.

The two components will be bound such that edit values are reflected on the display component (typically a TextBlock).

My desired syntax for the XAML is as follows.

     <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>

The code behind

 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();
    }
}

The error I'm getting is that EditableControlContainer.DisplayControl is not recognised or accessible.

I also am uncertain whether I can contain an object or type UserControl (preferably, I'd like to use an interface, although I'm not sure which one).

Can anybody explain or provide a link to a resource which demonstrates how to make properties within the UserControl configurable within the XAML?

I've looked through the forum, but have not been able to find anything which covers this.

Many thanks.

A TextBlock is not a UserControl, if you want to put a TextBlock in there, you need to change the type for display control to TextBlock.

private TextBlock displayControl;

public TextBlock DisplayControl {
    get { return displayControl; }
    set { displayControl = value; }
}

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