简体   繁体   中英

Bind view model to content control/presenter

on uwp, I want to make a hamburger menu with custom icon in xaml. from the lunch scheduler sample from MS, I try to bind a viewmodel property to a content element in a view like :

<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
                    <ListBox.ItemTemplate>
                        <DataTemplate x:DataType="viewmodels:MenuItem">
                            <StackPanel Orientation="Horizontal">
                                <ContentControl Content="{x:Bind Icon2}" />

where Icon is a property like

Icon2 = Application.Current.Resources["BookIcon"],

which works and retreive a viewbox containing a canvas

but I got an exception "Value does not fall within the expected range."

Did some know if it's possible like in WPF ??

nb : if i put the canvas directly in the contentcontrol, it works. perhaps the binding does not accept anything else than string

i found a solution, convert the viewbox to DataTemplate and bind to ContentTemplate

<DataTemplate x:Key="BookIcon">
            <Viewbox Width="48" Height="48">
                <Canvas Width="24" Height="24">
                    <Path Data/>
                </Canvas>
            </Viewbox>
        </DataTemplate>

and

<ContentControl ContentTemplate="{x:Bind Icon2}"/>

any other solution is welcome

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