views:

48

answers:

3

I have a grid with repeated columns with same style but different names the name are important for me in code behind so i decided to create a template to repeat the element and read the names from an XML file but i found out i can not use binding for name property please kindly help me sooooooooooooooooooooooooooon

here is a part of my code:

<DataTemplate  x:Key="weekItemTemplate">
            <Gridx:Name="{Binding XPath=Container}" Style="{DynamicResource GridStyle}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.9*"/>
                    <RowDefinition Height="0.1*"/>
                </Grid.RowDefinitions>
                <Rectangle x:Name="Rectangle"  Grid.Row="0" Margin="0,0,0,0.063"/>
                <TextBlock x:Name="Christian"  HorizontalAlignment="Left" Width="18.52" Grid.Row="1" Margin="1,0,0,0"><Run Text="08"/></TextBlock>
                <TextBlock x:Name="Hejri" Grid.Row="1"  HorizontalAlignment="Right" Width="9.773" Margin="0,0,0,0"><Run Text="۰۱"/></TextBlock>
                <TextBlock x:Name="Persian"  Margin="1,0,0,0" HorizontalAlignment="Left" Width="10"><Run Text="۰"/></TextBlock>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid Margin="0,10,0,0">
        <Grid.DataContext>
            <XmlDataProvider x:Name="TeamData" Source="weekdays.xml" XPath="days/day" />
        </Grid.DataContext>
        <ListView x:Name="TeamsListBox" DockPanel.Dock="Left"
                     ItemsSource="{Binding}"
                     ItemTemplate="{StaticResource weekItemTemplate}"
                     IsSynchronizedWithCurrentItem="True"
                     Visibility="Visible" SelectionMode="Single" />
+1  A: 

After thinking about it once more, you could define a new attached property for your columns and bind your names from the XML file to this property. Then in your code behind you can access this attached property instead of the name property. Does this help?!

EDIT:
I was talking about an attached dependency property. Something like this:

public static class ColumnProps
{
    public static readonly DependencyProperty MyCustomNameProperty =
        DependencyProperty.RegisterAttached(
            "MyCustomName",
            typeof(string),
            typeof(ColumnProps)
        );

    public static void SetMyCustomName(DependencyObject obj, string newValue)
    {
        obj.SetValue(MyCustomNameProperty, newValue);
    }

    public static string GetMyCustomName(DependencyObject obj)
    {
        return (string)obj.GetValue(MyCustomNameProperty);
    }
}

You can then set this property on your grid like so:

<Grid xmlns:local="...">
    <Grid.ColumnDefinitions>
        <ColumnDefinition local:ColumnProps.MyCustomName="someName" Width="100"/>
        <!-- ... -->
    </Grid.ColumnDefinitions>
    <!-- ... -->
</Grid>

where xmlns:local should refer to the assembly and namespace where the ColumnProps type is defined.

gehho
Do you mean i should define a dependency property?
Bahar
See my EDIT above...
gehho
OK, I just saw that your question has been edited, and now I see that my proposal does not do what you would like to achieve.
gehho
Why do you want to give the DataTemplate a name?!
gehho
sorry my code was wrong i wanted to set my grid name property not my template i did what you said but it seems that attached property does not work in data template is it true or my code is wrong?
Bahar
Attached properties should work in a DataTemplate. How should I tell you whether your code is wrong if you do not show us the code, nor the error message you get. I do not have my crystal ball at hand, you know... BTW what about some punctuation and casing in your sentences it is pretty hard to read otherwise take the time and add some dots or commas i would be pretty glad if you did so thanks a lot
gehho
A: 

You cannot bind to the name property (and thus change it at runtime), since it is used in the generated code (BAML and generated code-behind) as field names.

Daniel Rose
A: 

Code-behind wants names to be statically resolvable. That said, if the names are really dynamic, how could you write code that depends on the names in all cases? Perhaps there is a way you can modify the design so that you don't need to access the weekItem views directly, but can rather work with a class that models how the weekItem view behaves. For instance, if you need to register events or set certain properties, you could instead use a Command binding to a 'WeekItemViewModel' class and expose properties that reflect what you'd like the view to convey.

Once you do this, you've eliminated your dependency on specific names, which means you can dynamically load your data and instantiate these WeekItemViewModel classes, which insulate you from the details of how the view is actually laid out. Your code can focus more on how it wants to interact and less on how you wire up the code-behind to your specific visuals.

Dan Bryant