【问题标题】:WPF ListView item's Visible/Collaped not changing according to its VisibilityWPF ListView 项目可见/折叠不根据其可见性改变
【发布时间】:2014-05-28 11:11:02
【问题描述】:

感谢您花时间阅读我的帖子。

我在 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 组合为布尔值,然后将布尔值转换为可见性,但它没有用。我的意思是,Visiblity 仅在应用程序启动时折叠(应用程序加载 xml 以在启动时获取可见性)。之后,无论绑定变量(Visiblity)如何变化,值确实会变为Collapsed,但lblViewTiles仍在ListView控件中,UI没有变化。

以下是 DataContext 的绑定方式: ListView 的DataContex 绑定到CaptureSetupModules 类。 ListView 在LiveVM 类中定义。加载 xml 的动作在 MasterView 类中。所以为了访问CaptureSetupModules中的Visibility属性,我只是在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 都没有响应。该项目始终存在或不存在。

基本上问题是:绑定的变量发生了变化,xml文件也发生了变化,但是UI没有变化,当应用程序在加载xml时第一次启动时出现异常。

这里可能有点乱,如果您需要其他任何东西,请告诉我。我自己也很困惑。谢谢。

【问题讨论】:

    标签: wpf listview user-interface binding visibility


    【解决方案1】:

    在属性 XYCtrlVisible 上,您需要实现 INotifyPropertyChanged
    这就是通知 UI 更改的方式

    你为什么用这两种方式绑定?

    【讨论】:

    • 非常感谢。这是一个现有的代码,所以我尽量不要改变太多。关心分享一下如何实现 INotifyPropertyChanged?我真的是 WPF 的新手吗?
    • 只要搜索 msdn.microsoft.com msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 2012-12-04
    • 2020-12-10
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多