正如问题所述,我希望添加Click_Events
到Generic.Xaml
.
然而,这并不是每个人似乎都在问的典型问题,因为它并不那么简单。我的按钮是在运行时生成的,所有 35 个按钮都在 Calender-Control 上。日历上的每一天(方形)都将是一个按钮,单击该按钮将打开一个弹出表单,显示完整日期和更多详细信息。
这是日历控件的示例:
http://i.stack.imgur.com/il1V6.jpg
这是 XAML 代码:
<Button IsEnabled="{Binding IsEnabled}" Click="" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Auto" Background="{x:Null}" Foreground="White" Height="72" Width="131">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter
VerticalAlignment="Center"
HorizontalAlignment="Left">
<ContentPresenter.Content>
<Grid x:Name="ContentGrid" HorizontalAlignment="Left" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="15*" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" FlowDirection="RightToLeft">
<TextBlock Text="{Binding Date, Converter={StaticResource DateConverter}, ConverterParameter=DAY}" Grid.Column="0" VerticalAlignment="Top" FontSize="14" Margin="5,5,5,5" FontWeight="Light" >
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsTargetMonth}" Value="false">
<Setter Property="TextBlock.Foreground" Value="Black"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
<TextBlock Text="{Binding Notes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" VerticalAlignment="Center" FontSize="14"/>
</Grid>
</ContentPresenter.Content>
</ContentPresenter>
</ControlTemplate>
</Button.Template>
</Button>
正如您从 XAML 中看到的那样,有一个Click_Event
可用的,但我不能引用另一个表单,也不能命名按钮,因为会有几个,所以没有直接的方法来引用类中的方法。此外,我试图绑定,但一直给我一个空绑定错误。
目前,在 Calendar 类中,我有以下方法:
public void BuildCalendar(DateTime targetDate)
{
Days.Clear();
//Calculate when the first day of the month is and work out an
//offset so we can fill in any boxes before that.
DateTime d = new DateTime(targetDate.Year, targetDate.Month, 1);
int offset = DayOfWeekNumber(d.DayOfWeek);
if (offset != 1) d = d.AddDays(-offset);
//Show 5 weeks each with 7 days = 35
for (int box = 1; box <= 35; box++)
{
Day day = new Day { Date = d, Enabled = true, IsTargetMonth = targetDate.Month == d.Month };
day.PropertyChanged += Day_Changed;
day.IsToday = d == DateTime.Today;
Days.Add(day);
d = d.AddDays(1);
}
}
还有一个Day_Clicked
我似乎无法引用的方法(如果这是我需要做的):
private void Day_Clicked(object sender, EventArgs e)
{
if (Clicked == null) return;
Clicked(this, new DayClickedEventArgs(sender as Day));
}
自定义EventHandler
:
public event EventHandler<DayClickedEventArgs> Clicked;
活动类别:
public class DayClickedEventArgs : EventArgs
{
public Day Day { get; private set; }
public DayClickedEventArgs(Day day)
{
this.Day = day;
}
}
总而言之,我需要知道如何为Click_Events
日历中的每个生成按钮生成。Click_Events
用于使用Generic.Xaml
页面生成的按钮。