以编程方式将类绑定添加到带有事件处理程序的TextBlock

时间:2014-11-14 07:11:20

标签: c# .net wpf xaml .net-3.5

我试图在互联网上找到我的代码中的问题,但没有成功。我对WPF很新,我在从绑定类中读取值时遇到问题。这将是日历之后,这里的XAML为MainWindow.xaml:

<Grid Grid.Row="1" x:Name="grdCalendar" HorizontalAlignment="Right">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition x:Name="weekNo" Width="20"/>
                    <ColumnDefinition x:Name="day1st" Width="20"/>
                    <ColumnDefinition x:Name="day2nd" Width="20"/>
                    <ColumnDefinition x:Name="day3rd" Width="20"/>
                    <ColumnDefinition x:Name="day4th" Width="20"/>
                    <ColumnDefinition x:Name="day5th" Width="20"/>
                    <ColumnDefinition x:Name="day6th" Width="20"/>
                    <ColumnDefinition x:Name="day7th" Width="20"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition x:Name="dayName" Height="20"/>
                    <RowDefinition x:Name="week1st" Height="20"/>
                    <RowDefinition x:Name="week2nd" Height="20"/>
                    <RowDefinition x:Name="week3rd" Height="20"/>
                    <RowDefinition x:Name="week4th" Height="20"/>
                    <RowDefinition x:Name="week5th" Height="20"/>
                    <RowDefinition x:Name="week6th" Height="20"/>
                </Grid.RowDefinitions>
                <!--Days-->
                <Border Grid.Row="0" Grid.Column="1" Background="Chocolate" x:Name="brdElement1">
                    <TextBlock x:Name="dayName1" Text="Mo" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="0" Grid.Column="2" Background="BurlyWood" x:Name="brdElement2">
                    <TextBlock x:Name="dayName2" Text="Tu" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="0" Grid.Column="3" Background="OrangeRed" x:Name="brdElement3">
                    <TextBlock x:Name="dayName3" Text="We" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="0" Grid.Column="4" Background="Tomato" x:Name="brdElement4">
                    <TextBlock x:Name="dayName4" Text="Th" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="0" Grid.Column="5" Background="Crimson" x:Name="brdElement5">
                    <TextBlock x:Name="dayName5" Text="Fr" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="0" Grid.Column="6" Background="Gold" x:Name="brdElement6">
                    <TextBlock x:Name="dayName6" Text="Sa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="0" Grid.Column="7" Background="Salmon" x:Name="brdElement7">
                    <TextBlock x:Name="dayName7" Text="Su" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <!--Week No.-->
                ...
            </Grid>

这里的C#部分为MainWindow.xaml.cs:

private void setCalendar(int Month, int Year)
{
        // Show actual year
        actualYear.Text = year.ToString();

        // Show actual month
        switch (month)
        {
            case 1:
                monthName.Text = "January";
                break;
            case 2:
                monthName.Text = "February";
                break;
            case 3:
                monthName.Text = "March";
                break;
            case 4:
                monthName.Text = "April";
                break;
            case 5:
                monthName.Text = "May";
                break;
            case 6:
                monthName.Text = "June";
                break;
            case 7:
                monthName.Text = "July";
                break;
            case 8:
                monthName.Text = "August";
                break;
            case 9:
                monthName.Text = "September";
                break;
            case 10:
                monthName.Text = "October";
                break;
            case 11:
                monthName.Text = "November";
                break;
            case 12:
                monthName.Text = "December";
                break;
        }

        // Present month
        for (int i = 1; i <= 7; i++)
        {
            for (int j = 1; j <= 6 ; j++)
            {
                TextBlock txtDay = new TextBlock();

                // Binding
                Binding txtDayBinding = new Binding("Day");
                txtDayBinding.Source = new DayItem((i * 10) + j, 2, 2014, false); // Only test data
                txtDay.SetBinding(TextBlock.TextProperty, txtDayBinding);


                txtDay.Background = new SolidColorBrush(Colors.Tomato);
                txtDay.Foreground = new SolidColorBrush(Colors.Red);

                // Click Event
                txtDay.AddHandler(TextBlock.PreviewMouseDownEvent, new RoutedEventHandler(day_Click)); // PreviewMouseDownEvent, alternative to Button.ClickEvent

                Grid.SetRow(txtDay, j);
                Grid.SetColumn(txtDay, i);

                grdCalendar.Children.Add(txtDay);
            }
        }
    }

事件 - 尝试读取DayItem公共值。

private void day_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(((TextBlock)sender).Text);

        try
        {
            //MessageBox.Show( ((DayItem)((DayItem)sender)).Day.ToString() ); // Bad idea.
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

Binded DayItem Class。

class DayItem
{
    public DayItem(int Day, int Month, int Year, Boolean Bold = false)
    {
        this.Day = Day;
        this.Month = Month;
        this.Year = Year;
        this.Bold = Bold;
    }


    public int Day
    {
        get;
        private set;
    }

    public int Month
    {
        get;
        private set;
    }

    public int Year
    {
        get;
        private set;
    }

    public Boolean Bold
    {
        get;
        set;
    }

    public override string ToString()
    {
        return "DayItem";
    }
}

感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

对于day_Click(),我会将DayItem存储在TextBlock的Tag属性中,并在click事件中检索它:

// When you create your TextBlock
TextBlock txtDay = new TextBlock();
DayItem item = new DayItem((i * 10) + j, 2, 2014, false);
txtDay.Tag = item;
(...)
txtDayBinding.Source = item;



// In day_Click()
DayItem item = (DayItem)(((TextBlock)sender).Tag);

一些评论:

  • 在您的情况下,您似乎并不真正需要使用绑定,因为您不会保留实例并仅使用“白天”#39;属性。

  • 有关信息,我认为DataGrid旨在以另一种方式使用:禁用&#34; AutoGenerateColumns&#34;,并创建一个继承自&#34; DataGridBoundColumn&#34;或&#34; DataGridTextColumn&#34;。然后你覆盖&#34; GenerateElement()&#34;能够为DataGrid提供您的UIElement。

此致