DCSIMG
October 2007 - Posts - Just code - Tamir Khason

October 2007 - Posts

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/formatting-templates-and-one-small-tip/]


From internal discussion list:
Are there any samples around doing this kind of formatting with WPF?

clip_image002

My response: This should be very easy to do with value converter for Background brush of Item template.

How to do it?

First of all, let's understand what we want to archive? We want our gradientbrush to change it's end point, according to relative value passed to the item. So, let's do it

<DataTemplate x:Key="template">
      <TextBlock Text="{Binding}" >
        <TextBlock.Background>
          <LinearGradientBrush EndPoint="{Binding Converter={StaticResource converter}, ConverterParameter=1000}" StartPoint="0,0.5">
            <GradientStop Color="#FF7CA0CF" Offset="0"/>
            <GradientStop Color="#FF7CA0CF" Offset="0.8"/>
            <GradientStop Color="#FFFFFFFF" Offset="1"/>
          </LinearGradientBrush>
        </TextBlock.Background>
      </TextBlock>
    </DataTemplate>

In the code of converter we'll just translate values, according the max value, passed as parameter.

class DataConverter:IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int val = (int)value;
            int max = int.Parse(parameter.ToString());
            return new Point(val == 0 ? 0 : ((double)val / (double)max), 0.5);
        }

Very good. Compile and run. We get wrong result. Why? Due to fact, that TextBlock control does not resizes and stretches, according it's parent width. We can do it hardcoded for sure, but we want it automatically. Binding will help us another time. But this time, it's relative binding to ancestor's value.

<TextBlock Text="{Binding}" Margin="5,5,5,5" TextAlignment="Right" Padding="0,0,20,0"  Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=ActualWidth}">

That's all. We got the behavior, we looked for. See yourself.

image

Source code for this article.

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/visual-studio-2008-beta-2-is-crashing-install-this-patch/]


Most of clients, installed VSTS 2008 Beta 2 complained about rapidly crashing IDE. The core problem was found in IntelliSense feature of C# and yesterday new patch was released. So, if you are running into this problem and using English SKU, please install the patch to fix the problem.

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/wpf-perf-sessions-is-over-and-results-are-coming-whats-new-in-performance-case-in-net-35-and-30-sp1/]


After WPF performance sessions is over, one of WPF performance PMs, Josef Goldberg discovered in his blog about what are good news, will come with WPF (3.5 and 3.0/SP1). Here come the list:

  • Animation smoothing improvements (finally, it'll use DWM engines)
  • Layered Windows Improvements - fix was released about three months ago. This one will be included as well in SP1
  • Data Binding improvements - XMLDataSource will work faster
  • Coldstart time (hmmm, sounds strange, due to fact, that delays mostly come from CLR, not from WPF. But who knows)
  • Workset - less (or more???) space for future possibilities?
  • Software rendering improvements. We'll finally be able to do it manually.
  • Battery life improvements - well, not sounds good :)

But the real good news, that we where sounded. And this is the real value of this event.

Tnx, Yossi :)

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/life-before-starbucks-and-microsoft-vs-crapper/]


In case, you don't know, that it used to be life before Starbucks and Microsoft here the real-life evidence.

In case, that when you hear word "crapper", you are thinking about discarded cast offs. Here the other proof

Be good people - life is good, even with Jack-In-Box and Macy*s

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/focus-management-and-mouse-wheel-hooking-on-winforms-by-using-wpf/]


Someone asked very interesting question in MSDN forums. I was sure, that that's kind of "I forgot something small", however, I built small repro.


public partial class Window1 : System.Windows.Window
{

public Window1()
{
InitializeComponent();
UserControl1 uc = new UserControl1();
WindowsFormsHost wfh = new WindowsFormsHost();
wfh.Child = (System.Windows.Forms.Control)uc;


((
Grid)this.Content).Children.Add(wfh);

}

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}

const int WM_MOUSEWHEEL = 0x20A;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_MOUSEWHEEL)
label1.Text = (
int)m.WParam > 0 ? "Scrolling up" : "Scrolling down";
}

 

 

 

However, I noticed, that hosted WinForms control receives mouse notification only after it was clicked. What's the ****, I thought. It was not reasonable for me, so I hooked into WindowsFormsHost.Loaded and UserControl.Load event to set focus manually by code. 



....uc.Load +=
new EventHandler(uc_Load);
....
wfh.Loaded += new RoutedEventHandler(wfh_Loaded);

void wfh_Loaded(object sender, RoutedEventArgs e)
{
FocusManager.SetFocusedElement((DependencyObject)sender, (IInputElement)sender);
}

void uc_Load(object sender, EventArgs e)
{
((
UserControl1)sender).Focus();

}
 

No effect. The user control inside WindowsFormsHost got not focus. Digging deeper, I found interesting article about focus management in WPF Interop application. So, using System.Windows.Interop.IKeyboardInputSink.TabInto(System.Windows.Input.TraversalRequest)  solves the problem. Just one line of code and Interop control, hosted in WPF got the focus, that it requests.

((System.Windows.Interop.IKeyboardInputSink)sender).TabInto(new System.Windows.Input.TraversalRequest(FocusNavigationDirection.First));

Well, next time, I advice to MSDN doc team, to write small bullet and link to such simple solution, that has to occur (as for me) automatically, when I set focus to WindowsFormsHost WPF element.

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/welcome-to-israeli-bloggers-meeting/]


After 24 hours in air, directly from Redmond (where the real cold and rainy) I'm here, in the meeting of Israel's bloggers in Microsoft building in Ra'anana. Small buffet in the entrance and Lior with occasionally T-shirt. Let's see what MS marketing team want us to blog. He promise me that I'll not get my lost t-shirt today, but who cares.

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/microsoft-releases-the-source-code-for-net-base-classes/]


Wow, it's finally happening. Microsoft is releasing the source code for base classes of .NET framework. This includes ASP.NET, Windows Forms and WPF. Huge! Now you can break into debugger with the source code of .NET

More Posts « Previous page