感谢您抽出时间阅读我的帖子。
我在Windows 7 64位上使用VS2012,WFP和.net4.5
我在下面有一个带有xaml的ListView控件:
<ListView Name="lvViewerControl"
SelectionMode="Single"
SelectedIndex="{Binding Path=ViewType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Background="{x:Null}"
BorderBrush="{x:Null}"
Margin="2">
<Label Name="lblView2D"
Width="40"
Height="40"
Margin="3"
Background="#FF595959"
Foreground="White"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
<Image Source="/CaptureSetupModule;component/Icons/2D.png" />
</Label>
<Label Name="lblView3D"
Width="40"
Height="40"
Margin="3"
Background="#FF595959"
Foreground="White"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
<Image Source="/CaptureSetupModule;component/Icons/3D.png" />
</Label>
<Label Name="lblViewTiles"
Width="40"
Height="40"
Margin="3"
Background="#FF595959"
Foreground="White"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Visibility="{Binding Path=XYCtrlVisible, Mode=TwoWay, Converter={StaticResource boolToVis}}">
<Image Source="/CaptureSetupModule;component/Icons/Tile.png" />
</Label>
</ListView>
现在我要折叠第三项lblViewTiles
。我尝试将其Visibility
与bool组合,然后执行boo到可见性转换,但它不起作用。我的意思是不工作是Visiblity
仅在程序启动时崩溃。之后,无论绑定变量(Visiblity
)如何更改,并且值确实更改为Collapsed
,但lblViewTiles
仍位于ListView
控件中,则不会更改UI。
更新
ListView的DataContex
绑定到CaptureSetupModules
类,而不是LiveVM
。我只是在MasterView
类中创建了一个CaptureSetupModules,
在MasterView类
中 CaptureSetupModules _captureVM = new CaptureSetupModules();
...
LiveVM _liveVM = new LiveVM;
if (ndList.Count > 0)
{
xyBorder.Visibility = ndList[0].Attributes["Visibility"].Value.Equals("Visible") ? Visibility.Visible : Visibility.Collapsed;
tilesControlBorder.Visibility = ndList[0].Attributes["Visibility"].Value.Equals("Visible") ? Visibility.Visible : Visibility.Collapsed;
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
_captureVM.XYCtrlVisible = ndList[0].Attributes["Visibility"].Value.Equals("Visible") ? true:false;
}
)
);
}
这是我的转换器:
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var flag = false;
if (value is bool)
{
flag = (bool)value;
}
else if (value is bool?)
{
var nullable = (bool?)value;
flag = nullable.GetValueOrDefault();
}
if (parameter != null)
{
if (bool.Parse((string)parameter))
{
flag = !flag;
}
}
if (flag)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
此代码仅在程序启动时第一次折叠项目,并从xml文件加载可见性。之后,无论XYCtrlVisible(可见性绑定)如何更改,UI都不会显示响应。该项目始终存在,或者不存在。
这里可能有点乱,请告诉我你是否需要其他任何东西。我也很困惑。感谢。
答案 0 :(得分:1)
我建议您查看Converters
WPF
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
if (value is Boolean)
{
return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML
<StackPanel>
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="boolToVis" />
</StackPanel.Resources>
<CheckBox x:Name="chkShowDetails" Content="Show Details" />
<StackPanel x:Name="detailsPanel"
Visibility="{Binding IsChecked, ElementName=chkShowDetails,
Converter={StaticResource boolToVis}}">
</StackPanel>
可以找到更多信息here。