lynxSE
asked on
WPF - Bind button text to property
Hi,
i have datagrid with buttons (for cancel, update etc.) on each row and a button template corresponding to each button. The grid is bound to an observable collection containing order objects. The pause buttons content (text) is bound to a string propery in the order object and the content is supposed to change when the propery change.
How do i properly bind the content on the button to the string property?
button template:
<Style x:Key="PauseButtonTemplate " TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Button Name="PauseButton" Content="{Binding AlgoStatusButtonText, Mode=TwoWay}" Command="{Binding Path=DataContext.AlgoStatu sButtonCom mand, ElementName=xamDPTimeJeeve s}"
CommandParameter="{Binding }" Width="80" Height="20"></Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Datagrid:
<igDP:XamDataPresenter
x:Name="xamDPTimeJeeves"
Theme="Royale" DataSource="{Binding TimeOrders}" MaxHeight="400" Width="874">
<igDP:XamDataPresenter.Fie ldLayouts>
<igDP:FieldLayout Key="ParentLayout">
<igDP:FieldLayout.Fields>
<igDP:UnboundField Name="PauseOrder" Label="Pause Order" Column="0">
<igDP:UnboundField.Setting s>
<igDP:FieldSettings CellValuePresenterStyle="{ StaticReso urce PauseButtonTemplate}" CellHeight="12" CellWidth="50">
</igDP:FieldSettings>
</igDP:UnboundField.Settin gs>
</igDP:UnboundField>
Corresponding viewmodel:
void AlgoStatusButtonCommandExe cute(DataR ecord dataRecord)
{
if ((EAlgoStatusButtonText)da taRecord.C ells["Algo StatusButt onText"].V alue == EAlgoStatusButtonText.Stop )
{
_algoConnection.Stop(Conve rt.ToInt32 (dataRecor d.Cells["I nternalId" ].Value));
var order = (from o in TimeOrders where o.InternalId == Convert.ToInt32(dataRecord .Cells["In ternalId"] .Value) select o).ToList()[0];
TimeOrders[TimeOrders.Inde xOf(order) ].AlgoStat usButtonTe xt = EAlgoStatusButtonText.Rest art;
base.OnPropertyChanged("Ti meOrders") ;
}
Regards
//Robert
i have datagrid with buttons (for cancel, update etc.) on each row and a button template corresponding to each button. The grid is bound to an observable collection containing order objects. The pause buttons content (text) is bound to a string propery in the order object and the content is supposed to change when the propery change.
How do i properly bind the content on the button to the string property?
button template:
<Style x:Key="PauseButtonTemplate
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Button Name="PauseButton" Content="{Binding AlgoStatusButtonText, Mode=TwoWay}" Command="{Binding Path=DataContext.AlgoStatu
CommandParameter="{Binding
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Datagrid:
<igDP:XamDataPresenter
x:Name="xamDPTimeJeeves"
Theme="Royale" DataSource="{Binding TimeOrders}" MaxHeight="400" Width="874">
<igDP:XamDataPresenter.Fie
<igDP:FieldLayout Key="ParentLayout">
<igDP:FieldLayout.Fields>
<igDP:UnboundField Name="PauseOrder" Label="Pause Order" Column="0">
<igDP:UnboundField.Setting
<igDP:FieldSettings CellValuePresenterStyle="{
</igDP:FieldSettings>
</igDP:UnboundField.Settin
</igDP:UnboundField>
Corresponding viewmodel:
void AlgoStatusButtonCommandExe
{
if ((EAlgoStatusButtonText)da
{
_algoConnection.Stop(Conve
var order = (from o in TimeOrders where o.InternalId == Convert.ToInt32(dataRecord
TimeOrders[TimeOrders.Inde
base.OnPropertyChanged("Ti
}
Regards
//Robert
I'm assuming you don't need a basic tutorial, since you already have it bound, its just not updating.
Can you post some of the code for the TimeOrders? I'm assuming this is a collection of TimeOrder objects?
The AlgoStatusButtonText property in particular, does the class implement INotifyPropertyChanged, and if so, does the AlgoStatusButtonText property send out the proper notification?
Most of the time, when bindings aren't updating properly, it's because the bound target isn't actually receiving any notification that the source property has updated.
Can you post some of the code for the TimeOrders? I'm assuming this is a collection of TimeOrder objects?
The AlgoStatusButtonText property in particular, does the class implement INotifyPropertyChanged, and if so, does the AlgoStatusButtonText property send out the proper notification?
Most of the time, when bindings aren't updating properly, it's because the bound target isn't actually receiving any notification that the source property has updated.
ASKER
I have a object named clientorder with a property called AlgoStatusButtonText:
public class ClientOrder : IOrder
{
public ClientOrder()
{
}
public EAlgoStatusButtonText AlgoStatusButtonText{get; set; }
}
That object is "wrapped" in a viewmodel called ClientOrderViewModel which implements ViewModelBase which in turn implements INotifyPropertyChanged.
public class ClientOrderViewModel : ViewModelBase
{
private ClientOrder _clientOrder;
public ClientOrderViewModel()
{
_clientOrder = new ClientOrder();
AlgoStatusButtonText = EAlgoStatusButtonText.Stop ;
}
public EAlgoStatusButtonText AlgoStatusButtonText
{
get { return _clientOrder.AlgoStatusBut tonText; }
set
{
_clientOrder.AlgoStatusBut tonText = value;
OnPropertyChanged("AlgoSta tusButtonT ext");
}
}
It works if i put a propery in the viewmodel containing the ObservableCollection but not if i put the property in the ClientOrderObject.
Sorry if it seems messy, not sure if i implemented the MVVM pattern correctly.
TimeJeevesView -> TimeJeevesViewModel (with observable collection) -> ClientOrderViewModel (that wraps the object, with OnProperyChanged Method ) -> ClientOrderModel (object)
Regards
Robert
public class ClientOrder : IOrder
{
public ClientOrder()
{
}
public EAlgoStatusButtonText AlgoStatusButtonText{get; set; }
}
That object is "wrapped" in a viewmodel called ClientOrderViewModel which implements ViewModelBase which in turn implements INotifyPropertyChanged.
public class ClientOrderViewModel : ViewModelBase
{
private ClientOrder _clientOrder;
public ClientOrderViewModel()
{
_clientOrder = new ClientOrder();
AlgoStatusButtonText = EAlgoStatusButtonText.Stop
}
public EAlgoStatusButtonText AlgoStatusButtonText
{
get { return _clientOrder.AlgoStatusBut
set
{
_clientOrder.AlgoStatusBut
OnPropertyChanged("AlgoSta
}
}
It works if i put a propery in the viewmodel containing the ObservableCollection but not if i put the property in the ClientOrderObject.
Sorry if it seems messy, not sure if i implemented the MVVM pattern correctly.
TimeJeevesView -> TimeJeevesViewModel (with observable collection) -> ClientOrderViewModel (that wraps the object, with OnProperyChanged Method ) -> ClientOrderModel (object)
Regards
Robert
Odd, don't really see anything that should be messing that up...
Can you try putting a breakpoint in the AlgoStatusButtonText property in the viewmodel, and verify that it is actually being hit?
A couple other things... though I don't know where all you're using it, I'd suspect you don't really need a two-way binding on the Button's content property. TwoWays are typically used for input control to push changed values back to the source. It's not expected that you'd be changing the buttons text manually.
I personally am not a big fan of Infragistics and telerik and the control companies. Though taking nothing away from them (beautiful controls that ease a lot of dev work), a gigantic portion of the postings I see here and on other sites end up being due to unforseen glitches in their controls. A little suspicious that it's something like that.
Can you try putting a breakpoint in the AlgoStatusButtonText property in the viewmodel, and verify that it is actually being hit?
A couple other things... though I don't know where all you're using it, I'd suspect you don't really need a two-way binding on the Button's content property. TwoWays are typically used for input control to push changed values back to the source. It's not expected that you'd be changing the buttons text manually.
I personally am not a big fan of Infragistics and telerik and the control companies. Though taking nothing away from them (beautiful controls that ease a lot of dev work), a gigantic portion of the postings I see here and on other sites end up being due to unforseen glitches in their controls. A little suspicious that it's something like that.
ASKER
Ok, i will try to contact Infragistics to see if they have any pointers.
I will leave this question open in the mean time.
You are absolutely right about the two-way binding.
Thank you for your help so far.
Regards
Robert
I will leave this question open in the mean time.
You are absolutely right about the two-way binding.
Thank you for your help so far.
Regards
Robert
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Infragistics support team provided me with this solution.
http://coredotnet.blogspot
http://www.wpftutorial.net