UserControl 使用 ComboBox 更改矩形的可见性
UserControl changing Visibility for Rectangles with ComboBox
我在 UserControl 中有一个组合框,其中有 4 个选项可以更改主窗口中 4 个矩形的可见性。
当 Combobox 在 MainWindow 中时,我设法让它工作,但是当我将它移到 UserControl 时,它就不再工作了
我尝试放入 UserControl
MainWindow mw = new MainWindow();
放在前面
mw.blau_rec.Visibility = Visibility.Visible;
-> 这里我得到 Whosebug 错误
这是主窗口中矩形的 XAML:
<Rectangle x:Name="blau_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="Aqua"/>
<GradientStop Offset="1" Color="Blue"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="orange_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="Orange"/>
<GradientStop Offset="1" Color="OrangeRed"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="grey_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="#8E8E8E"/>
<GradientStop Offset="1" Color="#585858"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="light_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="#D5D5D5"/>
<GradientStop Offset="1" Color="#BEBEBE"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
这是UserControl.cs
中的代码
private void cbx_colorChanger_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var farbe1 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe1.Content.ToString() == "Blau")
{
blau_rec.Visibility = Visibility.Visible;
}
else
{
blau_rec.Visibility = Visibility.Hidden;
}
var farbe2 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe2.Content.ToString() == "Orange")
{
orange_rec.Visibility = Visibility.Visible;
}
else
{
orange_rec.Visibility = Visibility.Hidden;
}
var farbe3 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe3.Content.ToString() == "Grau")
{
grey_rec.Visibility = Visibility.Visible;
}
else
{
grey_rec.Visibility = Visibility.Hidden;
}
var farbe4 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe4.Content.ToString() == "Hell")
{
light_rec.Visibility = Visibility.Visible;
}
else
{
light_rec.Visibility = Visibility.Hidden;
}
}
这是用户控件中的组合框:
<ComboBox x:Name="cbx_colorChanger" Width="200" Height="25" SelectionChanged="cbx_colorChanger_SelectionChanged" >
<ComboBoxItem Name="cbx_blau_rec" Content="Blau"/>
<ComboBoxItem Name="cbx_orange_rec" Content="Orange"/>
<ComboBoxItem Name="cbx_grey_rec" Content="Grau"/>
<ComboBoxItem Name="cbx_light_rec" Content="Hell"/>
</ComboBox>
我需要一些帮助来解决这个问题,谢谢!
创建 MainWindow 的静态实例并在构造函数中将其设置为等于此值,如下所示:
public static MainWindow thisWindow;
public MainWindow()
{
InitializeComponent();
thisWindow = this;
}
现在您可以从任何地方访问主窗口:
MainWindow.thisWindow.blau_rec.Visibility = Visibility.Visible;
我在 UserControl 中有一个组合框,其中有 4 个选项可以更改主窗口中 4 个矩形的可见性。
当 Combobox 在 MainWindow 中时,我设法让它工作,但是当我将它移到 UserControl 时,它就不再工作了
我尝试放入 UserControl MainWindow mw = new MainWindow(); 放在前面
mw.blau_rec.Visibility = Visibility.Visible;
-> 这里我得到 Whosebug 错误
这是主窗口中矩形的 XAML:
<Rectangle x:Name="blau_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="Aqua"/>
<GradientStop Offset="1" Color="Blue"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="orange_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="Orange"/>
<GradientStop Offset="1" Color="OrangeRed"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="grey_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="#8E8E8E"/>
<GradientStop Offset="1" Color="#585858"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="light_rec" Visibility="Hidden">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0.2" Color="#D5D5D5"/>
<GradientStop Offset="1" Color="#BEBEBE"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
这是UserControl.cs
中的代码 private void cbx_colorChanger_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var farbe1 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe1.Content.ToString() == "Blau")
{
blau_rec.Visibility = Visibility.Visible;
}
else
{
blau_rec.Visibility = Visibility.Hidden;
}
var farbe2 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe2.Content.ToString() == "Orange")
{
orange_rec.Visibility = Visibility.Visible;
}
else
{
orange_rec.Visibility = Visibility.Hidden;
}
var farbe3 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe3.Content.ToString() == "Grau")
{
grey_rec.Visibility = Visibility.Visible;
}
else
{
grey_rec.Visibility = Visibility.Hidden;
}
var farbe4 = cbx_colorChanger.SelectedItem as ComboBoxItem;
if (farbe4.Content.ToString() == "Hell")
{
light_rec.Visibility = Visibility.Visible;
}
else
{
light_rec.Visibility = Visibility.Hidden;
}
}
这是用户控件中的组合框:
<ComboBox x:Name="cbx_colorChanger" Width="200" Height="25" SelectionChanged="cbx_colorChanger_SelectionChanged" >
<ComboBoxItem Name="cbx_blau_rec" Content="Blau"/>
<ComboBoxItem Name="cbx_orange_rec" Content="Orange"/>
<ComboBoxItem Name="cbx_grey_rec" Content="Grau"/>
<ComboBoxItem Name="cbx_light_rec" Content="Hell"/>
</ComboBox>
我需要一些帮助来解决这个问题,谢谢!
创建 MainWindow 的静态实例并在构造函数中将其设置为等于此值,如下所示:
public static MainWindow thisWindow;
public MainWindow()
{
InitializeComponent();
thisWindow = this;
}
现在您可以从任何地方访问主窗口:
MainWindow.thisWindow.blau_rec.Visibility = Visibility.Visible;