Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

entity framework - Binding issues with DataGrid WPF

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...