Tenhos 2表单,其中一个是usercontrol,一个是普通窗口。所以我将该usercontrol加载到我的窗口中。我想要做的是在单击用户控制按钮时更改窗口中的值。
窗口[用户控制]
当我点击用户控制按钮,例如“更改颜色窗口”时,我会改变窗口的背景颜色。
这怎么可能?
如果我创建一个全局类并放置一个类型为windows的对象,我将能够访问它吗?
答案 0 :(得分:1)
加载UserControl
后,您可以使用Window.GetWindow
方法从UserControl
获取对父窗口的引用:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window parentWindow = Window.GetWindow(this);
if (parentWindow != null)
parentWindow.Background = Brushes.Yellow;
}
}
答案 1 :(得分:0)
这很简单。您可以通过多种方式实现这一目标。我将列出简单的:
在UserControl
中创建一个事件,提升并聆听
在public
内,创建public Event EventHandler MyEvent;
///raise the event
private void btn_Click(object sender, RoutedEventArgs e)
{
MyEvent(this , e);
}
事件:
<强>&GT; UserControl.Cs 强>
Usercontrol1.MyEvent += new System.EventHandler(this.MyMethod);
pirvate void MyMethod
{
////do stuff here
}
<强>&GT; Wndows.Cs 强>
GetWindow
使用GetWindow
方法
您只需使用 private void btn_Click()
{
Window parent = Window.GetWindow(this);
///code here
}
方法:
StackPanel
使用VisualTreeHelper
这是一个可用于查找控件父级的函数。这非常有用,因为它不仅可以让您获得窗口,还可以获取孩子的任何父级,例如Grid
, public static T FindParent<T>(DependencyObject child)where T : DependencyObject
{
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
if (parentObject == null)
return null;
T parent = parentObject as T;
if (parent != null)
return parent;
else
return FindParent<T>(parentObject);
}
////Call it like this :
var parent = FindParent<Window>(this);
等。
library (highcharter)
rm(list=ls())
#### CASE 1 - X AXIS NAMES NOT SHOWING IN DRILLDOWN ####
h <- 0
Case1Num <- c()
for(i in 1:400){
h <- h + .02
Case1Num[i] <- h
}
Case1Name <- c()
for(i in 1:400){
Case1Name[i] <- paste0("test",i)
}
Case1Group <- c()
for(i in 1:400){
Case1Group[i] <- paste0("group1")
}
Case1 <- data.frame(Case1Name,Case1Num,Case1Group)
Lvl1 <- aggregate(Case1$Case1Num, by = list(Case1$Case1Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))
dfLvl2 <- arrange(data_frame(name = Case1$Case1Name,value = Case1$Case1Num),desc(value))
hc <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)
hc
#### CASE 2 - X AXIS NAMES SHOWING CORRECTLY IN DRILLDOWN ####
h <- 0
Case2Num <- c()
for(i in 1:50){
h <- h + .02
Case2Num[i] <- h
}
Case2Name <- c()
for(i in 1:50){
Case2Name[i] <- paste0("test",i)
}
Case2Group <- c()
for(i in 1:50){
Case2Group[i] <- paste0("group1")
}
Case2 <- data.frame(Case2Name,Case2Num,Case2Group)
Lvl1 <- aggregate(Case2$Case2Num, by = list(Case2$Case2Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))
dfLvl2 <- arrange(data_frame(name = Case2$Case2Name,value = Case2$Case2Num),desc(value))
hc2 <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)
hc2
希望这会有所帮助:)