How to embed in WPF

I had to bind the Grid DataContext

to the listview propertySelectedItem

I could do this without any problem ... However, since the Listview SelectedItem

stores an object, the XAML editor does not know the type it is working with, which raises the following warning: "Can't resolve property X in data context of type" object "

Is there a way to specify in one of the bindings that typeset the object (I searched for WPF casting but could not find any suitable resources)

Here's a relevant excerpt from my XAML:

<ListView x:Name="ListView" ItemsSource="{Binding LoginScreens}" Grid.Column="0" Grid.Row="0" SelectionMode="Single"/>
<Grid Grid.Column="1" Grid.Row="0" DataContext="{Binding SelectedItem, ElementName=ListView}">
    <Grid.RowDefinitions>
       /*rows*/
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      /*Columns*/
    </Grid.ColumnDefinitions>
    <TextBox Grid.Row="0" Grid.Column="1" Style="{StaticResource TextBoxStyle}" Text="{Binding Name}"/>
    <TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource TextBoxStyle}" Text="{Binding NameFr}"/>
    <TextBox Grid.Row="2" Grid.Column="1" Style="{StaticResource TextBoxStyle}" IsEnabled="False" Text="{Binding Filename}"/>
    <TextBox Grid.Row="3" Grid.Column="1" Style="{StaticResource TextBoxStyle}" IsEnabled="False" Text="{Binding SHA1}"/>
</Grid>

      

I tried the following syntax on TextBox bindings which didn't work:

<TextBox Grid.Row="0" Grid.Column="1" Style="{StaticResource TextBoxStyle}" Text="{Binding (manifestEditor:LoginScreen.Filename)}"/>

      

But this throws an error for the key to be empty in the ListDictionary, so either I am missing something or this is not the correct way.

EDIT:

I would like to use XAML as much as possible to handle this

+3


source to share


2 answers


Your code works great for me. Just adding something new and useful here is another way of defining binding to the SelectedItem:

    <Grid>
    <StackPanel>
        <ListView x:Name="ListView" ItemsSource="{Binding LoginScreens}" 
                  IsSynchronizedWithCurrentItem="True" 
                  SelectionMode="Single"/>
        <Grid DataContext="{Binding LoginScreens}">
            <StackPanel>
                <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>
                <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding NameFr}"/>
                <TextBox Grid.Row="2" Grid.Column="1" IsEnabled="False" Text="{Binding Filename}"/>
                <TextBox Grid.Row="3" Grid.Column="1" IsEnabled="False" Text="{Binding SHA1}"/>
            </StackPanel>
        </Grid>
    </StackPanel>
</Grid>

      



IsSynchronizedWithCurrentItem synchronizes the default CurrentItem of the CollectionView with the selected item in your control, and by choosing the DataContext Grid as the ObservableCollection LoginScreens, you will get updates according to the selection of the ListView.

+5


source


Unbind the SelectedItem to the ViewModel. Databind DataGrid datacontext to the same ViewModel. As long as the ViewModel knows what types it is working with, everything should work well.



0


source







All Articles