So I have a DataGrid
to represent a Movie entity. There is a Poster column, which contains a path to the poster of the movie.
I want to open a file dialog to choose a path to the poster instead of typing it manually. I tried to use horizontal StackPanel
inside a DataTemplate
inside a DataGridTemplateColumn
. Inside a StackPanel I have a TextBox
and a Button
.
I have no idea how to bind the Text property of a TextBox to Poster column of data context: Binding attribute is unaccessible in either TextBox or DataGridTemplateColumn and I can't access TextBox control inside a DataTemplate
by its name. I tried the Text="{Binding Poster}"
of a TextBox, but it doesn't work.
<DataGrid x:Name="DG"
AutoGenerateColumns="False"
ColumnWidth="*"
SelectionChanged="DG_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" IsReadOnly="False"/>
<DataGridTextColumn Binding="{Binding Title}" IsReadOnly="False" Width="200"/>
<DataGridTextColumn Binding="{Binding ReleaseYear}" IsReadOnly="False"/>
<DataGridTextColumn Binding="{Binding FranchiseId}" IsReadOnly="False"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Poster}" Width="150"/>
<Button x:Name="OpenPosterBTN"
Content="Выбрать"
Click="OpenPosterBTN_Click"
HorizontalAlignment="Right"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
MoviesEntities context;
public MoviesWindow()
{
InitializeComponent();
context = new MoviesEntities();
DG.ItemsSource = context.Movies.ToList();
}
I guess I'm missing important concepts of WPF that would help me understand how to solve this situation? Or is there any obvious alternatives to implement this functionality? I was thinking about making an update window, but i was considering it would be an overkill for just opening a file dialog.
I'm new to WPF and Entity Framework, so please don't judge too hard as if i haven't googled this question long enough.
Thanks in advance!
question from:
https://stackoverflow.com/questions/65928951/binding-issues-with-datagrid-wpf 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…