0

Given the following Xaml:

<Window.Resources>
    <System:String x:Key="StringValue"></System:String>
</Window.Resources>
    <Grid>
        <ComboBox Margin="137,101,169,183" ItemsSource="{Binding collection}" SnapsToDevicePixels="True" IsHitTestVisible="true">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                <CheckBox Command="{Binding CheckCommand}" IsChecked="{Binding IsChecked}" Content="{Binding Name}"/>
                    <TextBlock Text="{StaticResource StringValue}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>

What I want is for the Textblock Text to be bound to a static resource, that is databound to a value on the ViewModel. The issue is System.String appears to not allow databinding. ANybody know of a way to do this? For context, the TextBlock needs a different itemssource than that of its parent combobox.

Thanks.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Darren Young
  • 10,972
  • 36
  • 91
  • 150
  • Did you include alias to **mscorlib**? `xmlns:System="clr-namespace:System;assembly=mscorlib"` With it no errors appear. – kyrylomyr Jun 10 '11 at 08:37
  • look at my edit for a possible wrapper class which allows to detect the updates of the string – fixagon Jun 10 '11 at 09:17

2 Answers2

2

String doesnt allow binding because it is not a DependencyObject (and doesnt implement INotifyPropertyChanged)

but why dont you just bind directly to the Value in the ViewModel?

if you cannot bind to a ViewModel (think about RelativeSource with searching Parent type) you can implement a wrapper (which implements INotifyPropertyChanged to get the changes in the object)

Example wrapper class:

public class BindWrapper<T> : INotifyPropertyChanged
{
    private T _Content;
    public T Content
    {
        get
        {
            return _Content; 
        }
        set
        {
            _Content = value;
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("Content"));
        }
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

how to instantiate and bind in XAML:

<Window.Resources>
    <local:BindWrapper x:Key="wrapper" x:TypeArguments="System:String">
        <local:BindWrapper.Content>
            <System:String>huuu</System:String>
        </local:BindWrapper.Content>
    </local:BindWrapper>
</Window.Resources>
<TextBlock Text="{Binding Source={StaticResource wrapper}, Path=Content}" />
fixagon
  • 5,506
  • 22
  • 26
  • I can't bind direct to the value, as the textblock parent (combobox) has an itemsource of an observable collection within the VM. It won't allow me to seperate the Textblock from that. – Darren Young Jun 10 '11 at 08:41
  • @Darren Young, is it an `ObservableCollection` or `ObservableCollection` or something else? – Jodrell Jun 10 '11 at 08:45
0

To clarify, A System.String has no dependency properties so you can't bind it anything. I think you need a convertor so your TextBlock can bind to the View Model. What type of ObservableCollection do you have on the View Model?

EDIT If you just want to bind a simple string to the text property this is the wrong answer. If you want to bind to formatted text, read on.

I was having this problem before. I wanted to bind my TextBlock to a string resource in my properties. I ended up subclassing TextBlock to BindableTextBlock and making and a Convertor for string to an Inline list.

Question and Answers here.

It may seem a little involved, there ought to be an easier way. However I've resused the control several times whenever I've needed to bind to some formatted text and it works. Hopefully you can benefit from my work, and perhaps improve.

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124