use of x:Array in XAML

W

wpfRookie

Hello,
can soeone pls give an example where the use of x:Array Markup Extension is
used?

I tried to defines some strings to fill my ComboBox in XAML, but failed.

Tnx
wpf Rookie
 
P

Pavel Minaev

can soeone pls give an example where the use of x:Array Markup Extension is
used?

I tried to defines some strings to fill my ComboBox in XAML, but failed.

<x:Array Type="sys:String">
<sys:String>Foo</sys:String>
<sys:String>Bar</sys:String>
<sys:String>Baz</sys:String>
</x:Array>

And the above should work if you stick it inside
<ComboBox.ItemsSource>, for example. Or else define it in resources
with x:Key, and then use {StaticResource} to bind it to
ComboBox.ItemsSource.
 
W

wpfRookie

Am Mon, 4 May 2009 11:08:53 -0700 (PDT) schrieb Pavel Minaev:
<x:Array Type="sys:String">
<sys:String>Foo</sys:String>
<sys:String>Bar</sys:String>
<sys:String>Baz</sys:String>
</x:Array>

And the above should work if you stick it inside
<ComboBox.ItemsSource>, for example. Or else define it in resources
with x:Key, and then use {StaticResource} to bind it to
ComboBox.ItemsSource.

Hallo Pavel,

sorry, does not work, as I wrote already. I have

<x:Array x:Key="d" Type="{x:Type sys:String}">
<sys:String>Herr</sys:String>
<sys:String>Frau</sys:String>
</x:Array>

in the Resources, and then

<ComboBox ItemsSource="{StaticResource d}"></ComboBox>

Of cource, ItemSource wants an IEnumerable, which ArrayExtension is not.

Greetz
wpf Rookie
 
P

Pavel Minaev

sorry, does not work, as I wrote already. I have

  <x:Array x:Key="d" Type="{x:Type sys:String}">
    <sys:String>Herr</sys:String>
    <sys:String>Frau</sys:String>
  </x:Array>

in the Resources, and then

  <ComboBox ItemsSource="{StaticResource d}"></ComboBox>

The following complete XAML code works for me in XamlPad (i.e. when I
click on the ComboBox, I see "Herr" and "Frau" in the dropdown):

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Page.Resources>
<x:Array x:Key="d" Type="{x:Type sys:String}">
<sys:String>Herr</sys:String>
<sys:String>Frau</sys:String>
</x:Array>
</Page.Resources>
<ComboBox ItemsSource="{StaticResource d}"/>
</Page>

So, apparently, there's something else about your code that prevents
it from working. It's impossible to tell what it might be unless you
post it.
Of cource, ItemSource wants an IEnumerable, which ArrayExtension is not.

It needs not be. x:Array is in many ways magical, and this is one of
them. The property will see the actual IEnumerable array in this case,
not the extension.
 
W

wpfRookie

Am Mon, 4 May 2009 22:06:46 -0700 (PDT) schrieb Pavel Minaev:
Hello Pavel,
The following complete XAML code works for me in XamlPad (i.e. when I
click on the ComboBox, I see "Herr" and "Frau" in the dropdown):

tnx for trying this out. You are right, it works as expected, in XamlPad as
well s in VS. Maybe I found a problem in VS2008, because if I add another
resource BEFORE the array, it does not work any longer. For example this:

<Window.Resources>

<SolidColorBrush x:Key="br1" Color="Blue"></SolidColorBrush>

<x:Array x:Key="d" Type="sys:String">
<sys:String>Herr</sys:String>
<sys:String>Frau</sys:String>
</x:Array>

</Window.Resources>

<StackPanel>
<ComboBox ItemsSource="{StaticResource d}"></ComboBox>
</StackPanel>

does NOT work in VS2008. It works in XamlPad, however.

I fiddled a little bit around, and I found that it depends on whether the
x:Array is the FIRST resource added. If this is not the case, it does not
work!

Any ideas?




And:
It needs not be. x:Array is in many ways magical, and this is one of
them. The property will see the actual IEnumerable array in this case,
not the extension.

yes, yes,.... my fault. ProvideValue of the ArrayExtension class returns a
string array, which should work. I was confused by the error message VS
gave to me.

Greetz
wpf rookie
 
P

Pavel Minaev

tnx for trying this out. You are right, it works as expected, in XamlPad as
well s in VS. Maybe I found a problem in VS2008, because if I add another
resource BEFORE the array, it does not work any longer. For example this:

  <Window.Resources>

    <SolidColorBrush x:Key="br1" Color="Blue"></SolidColorBrush>

    <x:Array x:Key="d" Type="sys:String">
      <sys:String>Herr</sys:String>
      <sys:String>Frau</sys:String>
    </x:Array>

  </Window.Resources>

 <StackPanel>
     <ComboBox ItemsSource="{StaticResource d}"></ComboBox>
 </StackPanel>

does NOT work in VS2008. It works in XamlPad, however.

I fiddled a little bit around, and I found that it depends on whether the
x:Array is the FIRST resource added. If this is not the case, it does not
work!

Any ideas?  

A quick Google search shows that it is a fairly well-known problem.
The solution, apparently, is to be somewhat roundabout in how you bind
to the array. Specifically, try:

<ComboBox ItemsSource="{Binding Source={StaticResource d},
Path=Items}"/>

Also, how about filing it as a bug at Connect, so that hopefully it is
fixed eventually?

https://connect.microsoft.com/VisualStudio/Feedback?wa=wsignin1.0
 
P

Paul Werkowitz

Am Tue, 5 May 2009 08:44:49 -0700 (PDT) schrieb Pavel Minaev:
A quick Google search shows that it is a fairly well-known problem.

What search words did you use?

I am new to WPF and did not even imagine that this might be a known
problem. I thought I did something wrong. WPF concepts are strange,
sometimes.
The solution, apparently, is to be somewhat roundabout in how you bind
to the array. Specifically, try:

<ComboBox ItemsSource="{Binding Source={StaticResource d},
Path=Items}"/>

OK, use the Bindung extension. What is weird is the "Path=Items" part. How
in the world can this work? Arrays do not have Items. Where do you learn
that? Can you pls give a docu hint?

Shivering...
wpf Rookie
 
P

Pavel Minaev

Am Tue, 5 May 2009 08:44:49 -0700 (PDT) schrieb Pavel Minaev:


What search words did you use?

"wpf x:array resources"
I am new to WPF and did not even imagine that this might be a known
problem. I thought I did something wrong. WPF concepts are strange,
sometimes.

It looks more like a genuine bug than by-design.
OK, use the Bindung extension. What is weird is the "Path=Items" part. How
in the world can this work? Arrays do not have Items. Where do you learn
that? Can you pls give a docu hint?

Like I said, I've picked it from the Google search. Items is not a
property of array, of course, it's the property of ArrayExtension.
It's unclear why this is not needed when x:Array is the first in
Resources block, but needed otherwise...
 
W

wpfRookie

Am Tue, 5 May 2009 14:29:50 -0700 (PDT) schrieb Pavel Minaev:
Hello again,
"wpf x:array resources"
I did not find any interesting articles with that search. Maybe because I
get "access denied" errrs for some sites.
Like I said, I've picked it from the Google search. Items is not a
property of array, of course, it's the property of ArrayExtension.
It's unclear why this is not needed when x:Array is the first in
Resources block, but needed otherwise...

Tnx, I understand better now. With the syntax

<ComboBox ItemsSource="{StaticResource d}">

we use the Extensions ProvideValue - member to deliver the data. For an
ArrayExtension this should be the array itself.

With the explicit binding we bind to the Items property of the extension
class, which also has the data.

Greetz
wpf Rookie
 
P

Pavel Minaev

Am Tue, 5 May 2009 14:29:50 -0700 (PDT) schrieb Pavel Minaev:
Hello again,



I did not find any interesting articles with that search. Maybe because I
get "access denied" errrs for some sites.

When you get that, try looking at the Google-cached versions of those
pages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top