代码之家  ›  专栏  ›  技术社区  ›  haku

如何获取发出事件的项控件的datacontext?

  •  0
  • haku  · 技术社区  · 7 年前

    我在一个 items control .我想访问 data context 单击按钮的。我如何才能做到这一点?

    型号:

    public class RunYear
    {
        public RunYear(int year)
        {
            Year = year;
            Months = new Month[3];
        }
        public int Year { get; set; }
        public Month[] Months { get; set; }
    }
    
    public class Month
    {
        public int ColumnIndex { get; set; }
        public string MonthName { get; set; }
        // some other props
    }
    

    代码隐藏:

    public partial class MainWindow : Window
    {
        private ObservableCollection<RunYear> _years = new ObservableCollection<RunYear>();
        public ObservableCollection<RunYear> Years { get{return _years; } }
        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
            GenerateData();
        }
    
        private void GenerateData()
        {
            for (int i = 2010; i < 2015; i++)
            {
                var runYear = new RunYear(i);
                runYear.Months[0] = new Month() { ColumnIndex = 0, MonthName = $"Jan {i}" };
                runYear.Months[1] = new Month() { ColumnIndex = 1, MonthName = $"Feb {i}" };
                runYear.Months[2] = new Month() { ColumnIndex = 2, MonthName = $"Mar {i}" };
                Years.Add(runYear);
            }
        }
    
        public void OnClick(object sender, RoutedEventArgs args)
        {
            // how do I get the databound item?
            var cp = sender as ContentPresenter; //doesn't work
            var vm = cp?.Content as Month;
        }
    }
    

    XAML:

        <Grid>
        <ItemsControl Name="icYears" ItemsSource="{Binding Years}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="70" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="75"/>
                        </Grid.RowDefinitions>
                        <DockPanel Grid.Column="0" Grid.Row="0" >
                            <TextBox IsReadOnly="True" TextAlignment="Center" Text="{Binding Year}" />
                        </DockPanel>
    
                        <ItemsControl Grid.Column="1" Name="icMonths" ItemsSource="{Binding Months}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Grid >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="75"></RowDefinition>
                                        </Grid.RowDefinitions>
                                    </Grid>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemContainerStyle>
                                <Style>
                                    <Setter Property="Grid.Column" Value="{Binding ColumnIndex}" />
                                </Style>
                            </ItemsControl.ItemContainerStyle>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Button Click="OnClick"  Content="{Binding MonthName}"  Padding="2" Margin="2"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   15ee8f99-57ff-4f92-890c-b56153    7 年前

    你是说像这样吗?

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
        var vm = (sender as FrameworkElement).DataContext;