views:

35

answers:

1

Hi, this is my simple try-it application that create a grid with 2 rows. The 1st row's height is bound to a properties. The value I assigned to it only works at run-time. I tried to make it also work when design-time but I failed to do that (I used this thread to write my app).

Please help me to see what I miss. Thank you!

[Edit]

The reason why I do this is that I want to set dynamically the height of the top grid row, ie. Grid.Row="0", to be the title bar height. Somewhere in my app, the view loaded and overlap the title bar.

+1  A: 

You're trying to do a very strange trick, which is not supposed to work. Try to make the following changes.

MainWindow.xaml.cs -- try to always keep you code-behind clear.

namespace WpfTryIt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

MainWindow.xaml

<Window x:Class="WpfTryIt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"

        xmlns:s ="clr-namespace:WpfTryIt"
        >
    <Window.DataContext>
        <s:FakeDataContext></s:FakeDataContext>
    </Window.DataContext>


        <Button Content="{Binding Path=BindingHeight}"/>

</Window>

And a new separate data context class, which behave different depending on the mode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace WpfTryIt
{
    public class FakeDataContext
    {
        public int BindingHeight
        {
            get
            {
                // Check for design mode. 
                if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
                {
                    //in Design mode
                    return 100;
                }
                else
                {
                    return 200;
                }
            }
        }
    }
}
Yacoder
@Yacoder: In case the FakeDataContext's constructor require some arguments, how can I modify your codes to make it works?
Nam Gi VU
@Yacoder: I've also update my question to explain why I try the `strange trick`.
Nam Gi VU
@Nam Gi VU: In XAML you can only create objects that have empty constructor. If you still want to instantiate an object with parameters, check out this question http://stackoverflow.com/questions/2335900/using-xamlreader-for-controls-that-does-not-have-a-default-constructor
Yacoder