xaml ()----原代码实现checkbox 打勾与取消勾便执行Visible代码(此代码不能改,为已使用的软件截图片断)。现新增Label 1,想关联此checkbox 或visible,实现打勾Label1显示,不打勾则Label1隐藏.
我试过在visible中加插Label1.visibility=visibility.collapsed;提示出错:无法通过嵌套类型访问外部类型非静态成员。
也试过:将label1所在的类实例化引用 usrprocesschart up=new usrprocesschart(); up.Label1.visibility=visibility.collapsed; 但usrprocesschart 太多内容,导致很慢,此方法不理想。
请问用wpf技术在xaml中解决此问题可行吗,如何写?或者有其它办法吗?谢谢!
<Window.Resources>
<Style x:Key="chkLstItemContainerStyle" TargetType="ListBoxItem" BasedOn="{StaticResource ControlStyle}">
<Setter Property="Margin" Value="2,1,2,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<CheckBox Margin="{TemplateBinding Margin}"
Background="{TemplateBinding Background}"
FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
IsChecked="{Binding IsSelected,RelativeSource={RelativeSource TemplatedParent}}">
<StackPanel Orientation="Horizontal">
<Rectangle Height="3" Width="20" Margin="4" Fill="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<ContentPresenter/>
</StackPanel>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox x:Name="chkLstSignal" SelectionMode="Multiple" BorderBrush="Gray" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource chkLstItemContainerStyle}">
<Setter Property="IsSelected" Value="{Binding Visible}"/>
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{Binding Color2}"/>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
cs中关于Visible代码:
private bool _Visible = false;
public bool Visible
{
get { return _Visible; }
set
{
if(_Visible !=value)
{
_Visible = value;
SetVisible(_Visible);
OnPropertyChanged("Visible");
}
}
}
protected override void SetVisible(bool b)
{
foreach (LineGraph l in Lines)
{
l.Visibility = Visible ? Visibility.Visible : Visibility.Hidden;
}
}
假设CheckBox的IsCheck绑定到字段CheckStatus上,然后Label1的Visible绑定CheckStatus并且给绑定增加一个Convert去转换bool值到Visibility。