English 中文(简体)
如何以更加划清的方式做到这一点? (图象清单,以确定身份)
原标题:how could i do this in a smarter way ? (list of images to set a status on)
  • 时间:2012-01-13 19:16:47
  •  标签:
  • c#
  • list

I m still in the beginning stage of learning c#, so i have a question about a part of my code. I strongly have the feeling there should be a way smarter way to accomplish what i try to do:

So what do i have: A WPF window, with a whole bunch of images on it. At the start, i want them all to be set to Hidden, so at the initialize part of the window, i set them all hidden and do it like this:

.....
IMx01y01W.Visibility = Visibility.Hidden; 
IMx23y73W.Visibility = Visibility.Hidden;
IMx31y21W.Visibility = Visibility.Hidden;
IMx03y16W.Visibility = Visibility.Hidden;
.....

诸如IMx#y#y#W等图像的名称是合乎逻辑的,其中第2号是变数。

如上所述,我强烈认为,这样做确实应该有某种细致的方法。

=======

EDIT 页: 1

至今为止。

    foreach (object obj in LogicalTreeHelper.GetChildren(this))
    {
        if (obj is Image)
        {
        Image obj = (Image)item;
        obj.Visibility = Visibility.Hidden;
        }

    //do something
    }

The part in the if statement is totally wrong, but i don t know how to go on on this point. can anyone push me a bit more in the right direction ? thanx!

最佳回答

You can frame this requirement as "set the Visibility property of the various images to the same value", i.e. they all show/hide based on the same exact flag. WPF has some mechanisms that require a slightly different mindset than traditional imperative code, but can greatly simplify this scenario.

WPF支持以下概念:数据,允许你宣布图像依赖价值,而不是使用手头价值。 您可以利用这一优势来管理“<代码>Visibility仅作标记的财产价值。

了解情况的主要方面是:DataContext Property ,这是目前受特定控制的对象。 在这种情况下,你可将数据环境确定为<代码>值。 可见

public MainWindow()
{
    DataContext = Visibility.Hidden;
}

一旦你这样做,它就在整个树苗中 cas,那么所有孩子都继承了这一价值。 这意味着你可以在你的XAML中这样做:

<Image x:Name="IMx01y01W" Visibility="{Binding}" />
<Image x:Name="IMx23y73W" Visibility="{Binding}" />

这告诉世界森林基金会,你想要<代码>Visibility property,以反映目前<代码>DataContext/code>的价值,即Visibility。 Hidden <>/code> 贵方对构造中的数值。

与此相关的一点是,如果你改变<代码>DataContext 财产的价值,将自动反映在你的所有图像中,而没有任何额外工作:

private void MakeImagesVisible()
{
    DataContext = Visibility.Visible;

    // At this point, all images will be visible
}

关键取而代之的是,世界森林论坛以不同于传统的核心模式,例如Windows表格。 学习这些新技术背后的哲学,而不是复制遗产技术,将有助于世界森林论坛更容易理解和理解你的意愿。

问题回答

你可以做的事情是:

IMx01y01W.Visibility = IMx23y73W. 可见度 = 可见度。 隐蔽;

但我只字不提这一点。

相反,你可以做的是,收集IMx01y01W物体(其基类),并通过这种可识别藏匿的收集环境加以净化。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签