[英]How do I access elements in a WPF popup?
I want to change the color of a button in a popup based on certain conditions and I want to set some text based on those conditions. 我想根据某些条件更改弹出窗口中按钮的颜色,并希望根据这些条件设置一些文本。 I need to do this in the code behind.
我需要在后面的代码中执行此操作。
I have a popup with several TextBlocks in a StackPanel. 我在StackPanel中有几个TextBlocks的弹出窗口。 The first 3 are bound to details about the course (this is a scheduling app; school project).
前三个绑定到有关该课程的详细信息(这是一个调度应用;学校项目)。 The last one I want to be empty unless there is a conflict concerning that course.
除非关于该课程有冲突,否则我最后一个要空。 That is, I want to dynamically decide what, if anything, goes in the TextBlock each time the popup is opened.
也就是说,我想动态决定每次打开弹出窗口时TextBlock中有什么内容。
<Popup Name="CourseListDetail" Width="Auto" Height="Auto">
<StackPanel>
<TextBlock Margin="10,10,10,0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}: {1}">
<Binding Path="CourseCode"/>
<Binding Path="LongTitle"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Margin="10,0,10,0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}/{2}">
<Binding Path="ProfessorsString"/>
<Binding Path="Enrollment"/>
<Binding Path="Capacity"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Margin="10,0,10,0" Grid.Row="2" Grid.Column="1" Text="{Binding MeetingsString}" TextWrapping="Wrap"
HorizontalAlignment="Center"/>
<TextBlock TextWrapping="WrapWithOverflow" MaxWidth="300" Text="{Binding Description}" Margin="10,10,10,10"/>
<TextBlock Name="ConflictText" Foreground="Red" HorizontalAlignment="Center" Text="{Binding ConflictString}"/>
<Button Name="Detail_AddCourse" Content="Add To Schedule" Margin="10,10,10,10" Padding="5" Background="LightGreen"
Click="AddCourseButton_Click"
Style="{StaticResource MyButtonStyle}"/>
</StackPanel>
</Border>
</Popup>
I have a function that opens the popup when you click on a course and that gives the popup the DataContext about the course, but I don't know how to access the TextBlock, or the button immediately below it, through the function. 我有一个函数,当您单击课程时会打开弹出窗口,并为弹出窗口提供有关课程的DataContext,但是我不知道如何通过该函数访问TextBlock或它下面的按钮。 I figured there'd be a child property or something so I could call the button, something like:
我发现会有子属性或其他东西,所以我可以调用按钮,例如:
CourseListDetail.Detail_AddCourse.Background = "Red";
or 要么
CourseListDetail.Child.Button().Background = "Red";
etc. 等等
Code behind function: 功能背后的代码:
private void CourseListItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ListViewItem selection = sender as ListViewItem;
Course course = selection.DataContext as Course;
CourseListDetail.DataContext = course;
CourseListDetail.PlacementTarget = selection;
CourseListDetail.IsOpen = true;
CourseListDetail.Focus();
hasConflict conflictType = _schedule.HasConflict(course);
if (conflictType != hasConflict.NO_CONFLICT) { //If there is a conflict
//Change button color to red here
if (conflictType == hasConflict.COURSE_FULL) { //If the course is full
//Set TextBlock text to conflict message here
}
}
else { //No conflict
//Set button color to green
}
}
hasConflict is just an enum hasConflict只是一个枚举
Use x:Name instead of Name. 使用x:Name而不是Name。 Then you will be able to access the element in the code behind.
然后,您将可以访问后面代码中的元素。 See In WPF, what are the differences between the x:Name and Name attributes?
请参阅WPF中的x:Name和Name属性有什么区别? for an explanation.
进行解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.