Bind RibbonComboBox.SelectionBoxItem - wpf

I'm trying to bind the item selected in a RibbonComboBox to a property of an object. The problem I'm encountering is that the RibbonComboBox.SelectionBoxItem only provides a get accessor; therefore, I cannot bind it to anything in the XAML.
Any ideas how to bind the item to the property of an object? I could use a regular ComboBox is there another more appropriate control?
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
<ribbon:RibbonComboBox
ItemsSource="{Binding Source={StaticResource CollectionOfPossibleChoices}}"/
SelectionBoxItem="{Binding Path=PropertyToBindTo}"/> <!--Not valid-->

RibbonComboBox is unlike ComboBox (which i, also, find confusing). Try this;
<ribbon:RibbonComboBox>
<ribbon:RibbonGallery SelectedItem="{Binding Path=PropertyToBindTo}">
<ribbon:RibbonGalleryCategory ItemsSource="{Binding Source={StaticResource CollectionOfPossibleChoices}}" />
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>
MSDN Reference

Related

Adding resource in code and using it as StaticResource

For some reason I am unable to bind to ViewModel properties within DataTemplates on some controls. The result of the binding itself is unpredictable, sometimes it work, sometimes it doesn't. For this reason I am thinking of exposing the ViewModel in some other way besides setting it as DataContext.
First thought was to add ViewModel to Resources collection. I am using TabControls for UI, so whenever a view needs to be displayed, it is done through Data templates.
<DataTemplate DataType="{x:Type vm:SomeViewModel}">
<vw:SomeView />
</DataTemplate>
In this situation the view is instantiated automatically, and its DataContext is set to ViewModel set in template. Is there a way I can make this ViewModel available to View's Resources (ex with key=viewModel), so that I can use it like this:
<TextBlock Text="{Binding SomeProperty, Source={StaticResource viewModel}}" />
I have tried adding it in code, in the Loaded event for the View:
this.Loaded += (s, e) =>
{
this.Resources.Add("viewModel", this.DataContext);
};
Above code is executed before the error pops up that says static resource is not found at run-time, so the resource was added to collection.
Any ideas what can I do?
You can define ViewModel as a resource in XAML like that:
<vm:SomeViewModel x:Key="ViewModel"/>
If you want to Bind to DataContext in a DataTemplate you can use the following:
{Binding Path=DataContext, ElementName=uc}
assuming that your window/usercontrol name is x:Name="uc" , or as #stukselbax wrote:
{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=[UserControl|Window]}}

bind data to combobox in datagrid in silverlight

how can i bind the data returning from WCF in combox in grid . the combobox is not in edit mode. I am able to display the static data but not the data returning from WCF.
u can use RelativeSource to search needed DataContext/ViewModel/Page/UserControl in the elements tree
example for the ViewModel where DataGrid itemssource defined near ComBoxCollectionSource:
<ComboBox ItemsSource={Binding RelativeSource={RelativeSource AncestorType=sdk:DataGrid}, Path=DataContext.ComboBoxColloctionSource} />
I'm assuming the static data is the stuff you enter into the Items property by hand. The syntax for a combobox is a bit different, but here it is:
<ComboBox ItemsSource="{Binding Path=<your collection>,Mode=OneTime}" SelectedValuePath="<id-field>" DisplayMemberPath="<display-field>" SelectedItem="{Binding Path=<your-property>,Mode=TwoWay}" />
The parameters ending in PATH above just have the names of the properties as a string.
EDIT: If you're using a dictionary, you would use:
<ComboBox ItemsSource="{Binding Path=<your dictionsry>,Mode=OneTime}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedItem="{Binding Path=<your-int-property>,Mode=TwoWay}" />

Bind a ComboBox to two DataContexts

I have a ComboBox in my wpf application.
It's ItemsSource is binded to some table in my DataSet.
I need the text property to be binded to another's object property . I doesn't work because the ComboBox doesn't want to get two DataContexts. How can I solve this problem?
<StackPanel Width="Auto" Height="Auto" MinWidth="296" Orientation="Vertical" x:Name="MyStackPanel">
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding}" Text={Binding Path=MyProperty} />
</StackPanel>
In the code behind :
MyComboBox.DataContext = MyDataSet.Tables[MyTable];
MyStackPanel.DataContext = MyObject;
I want the ComboBox to show items from one DataContext but to show the text from another DataContext. How can I do it?
Don't use DataContext. Set the Source property of your bindings in XAML or create the bindings in code and set the Source property there.
Why are you assigning something to the datacontext of the stackpanel? From the looks of it, its not used.
Your code should work if MyDataSet.Tables[MyTable] returns an enumeration and contains a property called MyProperty.
What do you mean when you say that the combobox "doesn't want to get two DataContexts"?
Look into the properties IsEditable and IsReadOnly of the combobox.
Something like
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding}" Text={Binding ElementName=MyStackPanel Path=DataContext.MyProperty} />

WPF StaticResource bound to subproperty

I have a class thats created as a resource:
<Window.Resources>
<Model:MyModel x:Key="model" />
</Window.Resources>
The MyModel class has a cli property named Foo. I want the selected value in a combobox to be bound to this property. I thought I could do this but Im getting errors:
<ComboBox SelectedItem="{Binding Source={StaticResource model.Foo}}" />
Heres the error:
Cannot find resource named '{model.Foo}'.
Where did I go wrong? What extra parameters do I need to specify to properly bind to a subproperty?
You almost have it correct. You want to use a combination of the Binding's Path property and its Source property. So use one of the following (they are equivalent.)
{Binding Foo, Source={StaticResource model}}
or
{Binding Path=Foo, Source={StaticResource model}}
Hope this helps.

How do I databind to the control's property rather than the datacontext?

I have a sub control embedded inside my main control, it allows the user to edit an address. Because this is reused all over the place (sometimes in multiple places on one control) I bind it like so
<Controls:EditAddressUserControl DataContext="{Binding Path=HomeAddress}"/>
<Controls:EditAddressUserControl DataContext="{Binding Path=WorkAddress}"/>
But the EditAddressUserControl needs access to the main control's list of CountrySummary objects so it can choose which country the address belongs to.
I have added a Countries DependencyProperty to EditAddressUserControl and added
Countries="{Binding Countries}"
So far all is going well, the EditAddressUserControl.Countries property has the correct countries in it. However, how do I databind my Combobox.ItemsSource to that in XAML?
I still want everything on my EditAddressUserControl to bind to its DataContext, but the ComboBoxCountries.ItemsSource needs to bind to "this.Countries".
How do I do that?
I've tried this
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:EditAddressUserControl}}, Path=Countries}" />
I saw no binding errors in the output box, but I also saw no items in the combobox.
You can accomplish this by using a RelativeSource for the binding source, instead of the DataContext.
This would most likely look something like:
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:EditAddressUserControl}}, Path=Countries}" />
The way to do it was to stop using DataContext completely. Instead I added a DependencyProperty to my control
public static DependencyProperty AddressProperty = DependencyProperty.Register("Address", typeof(EditPostalAddressDto), typeof(EditPostalAddressControl));
Then in the parent control instead of setting DataContext="..." I set Address="..." - The XAML for the control is then changed to include an ElementName on the binding
<UserControl ..... x:Name="MainControl">
<TextBox Text="{Binding ElementName=MainControl,Path=Address.Region}"/>
Now I can specifically bind to the Address property, but also bind to properties on the main data context.

Resources