【发布时间】:2014-05-23 03:44:46
【问题描述】:
感谢您花时间阅读我的帖子。
我在 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 组合成一个布尔值,然后将 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 都没有响应。该项目始终存在或不存在。
这里可能有点乱,如果您需要其他任何东西,请告诉我。我自己也很困惑。谢谢。
【问题讨论】:
-
代码中不起作用的部分在哪里?我没有看到任何
Visibility触发器 -
我修改了你指出的帖子,
标签: wpf listview events binding visibility