|
Thank You For reply but I have tried it and bind this value to property in view model and added breakpoint to that but after selecting multiple items command didn't break at that point therefore multiple items didn't get there
|
|
|
|
|
I use Telerik but I am certain the same binding works for all grids
Xaml
<telerik:RadGridView x:Name="tkGrid" Grid.Row="2" ItemsSource="{Binding EntityList,Mode=TwoWay}" SelectedItem="{Binding SelectedEntity,Mode=TwoWay}" >
<telerik:RadGridView.Columns> etc...
in the VM I have an observable collection and a selected item
#region -------- Entity selected item<br />
private EntityDB _SelectedEntity;
public EntityDB SelectedEntity
{
get
{ return _SelectedEntity; }
set
{
if (_SelectedEntity == value)
{ return; }
var oldValue = _SelectedEntity;
_SelectedEntity = value;
base.RaisePropertyChanged("SelectedEntity");
}
}
#endregion -------- Entity
This works for single item selection. For multiple selected items I have to use code behind to get the SelectedItems collection of the grid and pass it to the VM
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Here's my XAML
<ComboBox Grid.Row="0"
Grid.Column="1"
ItemsSource="{Binding Clients}"
DisplayMemberPath="ClientName"
SelectedValuePath="Id"
SelectedValue="{Binding SelectedClientId}"
Margin="2"
Width="200"
HorizontalAlignment="Left"/>
The combo is bound to an ObservableColection<cliententity> called Clients, The collection is being loaded and the data is valid. No binding errors.
When I run it I don't see anything in the list.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Is the list populated but not displaying text - typo in the field name
SelectedValue should probably be binding to SelectedClient.ID and should be two way
If there is nothing in the combobox at all then it is probably a typo in the list name (I assume there is data in the viewmodel)
Also check that the datacontext connects to your viewmodel.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
All of this checks out OK. No typos, and there is data. The DataContext is connected
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Well I just figured it out...
The MainWindowView has
<DataTemplate DataType="{x:Type vms:MyViewModel}">
<vw:MyView/>
</DataTemplate>
The VM's are based off a subclass that has a Load method. In the MainWindowViewModel when a view is selected I create MyViewModel and call the Load, which does
private void LoadClientsList()
{
var clients = _dal.GetClients();
Clients = new ObservableCollection<ClientEntity>(clients);
}
This all worked fine.
I just found this in MyView:
<UserControl.DataContext>
<vms:BacklogViewModel/>
</UserControl.DataContext>
This second call was recreating the VM, so the Clients collection was reset to null;
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Kevin Marois wrote: <DataTemplate DataType="{x:Type vms:MyViewModel}"> Ah he thinks there may be a reason why I have not seen that construct before.
I have to admit I only ever instantiate a datacontext from the view.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have created a style for a listbox:
ItemContainerStyle
<Style x:Key="listBoxItemContainerStyle"
TargetType="ListBoxItem">
<pre>
<Setter Property="Padding" Value="2"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Orange"/>
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Orange"/>
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
</Style.Triggers>
ListBoxStyle
<Style x:Key="listBoxStyle"
TargetType="ListBox">
<pre>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBackgroundBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource listBoxItemContainerStyle}"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="BorderBrush" Value="{StaticResource ButtonDisabledBackgroundBrush}"/>
<Setter Property="Control.BorderThickness" Value="2"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
Usage
<ListBox Grid.Row="1"
Grid.Column="4"
Style="{StaticResource listBoxStyle}">
<pre>
<ListBoxItem Content="One" IsSelected="True"/>
<ListBoxItem Content="Two" IsEnabled="False"/>
<ListBoxItem Content="333333"/>
When I run it BEFORE I click an item, the background of the first item is lightgray. Notice that the first item has IsSeledted= true.
If I click on it, then the background color turns Blue.
What's wrong here?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Hello,
the problem is that the default control template for ListBoxItem contains a border element which controls the background color. To fully control the appearance you could replace the template using the template from ListBoxItem ControlTemplate Example[^] with your own definition. See below for an example.
<Style x:Key="listBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="Orange"/>
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Gray"/>
<Setter TargetName="Border" Property="BorderThickness" Value="2"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="Orange"/>
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
|
|
|
|
|
I'm trying to create a simple Watermark TextBox. I first created a TextBox class with a WatermarkText property:
public class MaroisTextBox : TextBox
{
#region DP WatermarkText
public static readonly DependencyProperty WatermarkTextProperty =
DependencyProperty.Register("WatermarkText",
typeof(string),
typeof(MaroisTextBox),
new PropertyMetadata("", new PropertyChangedCallback(OnWatermarkTextChanged)));
public string WatermarkText
{
get { return (string)GetValue(WatermarkTextProperty); }
set { SetValue(WatermarkTextProperty, value); }
}
private static void OnWatermarkTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MaroisTextBox control = (MaroisTextBox)d;
control.SetWatermark();
}
#endregion
#region Overrides
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
SetWatermark();
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
SetWatermark();
}
#endregion
private void SetWatermark()
{
if (IsFocused)
{
if (Text.CompareTo(WatermarkText) == 0)
{
Text = string.Empty;
}
}
else
{
if (string.IsNullOrEmpty(Text))
{
Text = WatermarkText;
}
}
}
}
Next I created a style for it
<Style x:Key="SearchTextBoxStyle"
TargetType="TextBox">
<pre>
<Setter Property="FontFamily" Value="{StaticResource AppFontName}"/>
<Setter Property="FontSize" Value="{StaticResource NormalFontSize}"/>
<Setter Property="FontStyle" Value="Normal"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="Height" Value="30"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontStyle" Value="Italic"/>
</Trigger>
</Style.Triggers>
And its usage
<MaroisTextBox Grid.Column="3"
Width="200"
Margin="20,2,2,2"
Text="{Binding SearchText}"
WatermarkText="Search..."
Style="{StaticResource SearchTextBoxStyle}"/>
When I click into the textbox, the code behind runs as expected. Yet I don't see anything in the textbox.
When I click out, the watermark text appears, but in black and not italic.
I don't see why this won't work. Anyone see what's wrong here?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Kevin Marois wrote: When I click into the textbox, the code behind runs as expected. Yet I don't see anything in the textbox.
Which is what you told it to do:
Kevin Marois wrote:
if (IsFocused)
{
if (Text.CompareTo(WatermarkText) == 0)
{
Text = string.Empty;
}
}
Kevin Marois wrote: When I click out, the watermark text appears, but in black and not italic.
Which is also what you told it to do:
Kevin Marois wrote:
<Setter Property="FontStyle" Value="Normal"/>
<Setter Property="Foreground" Value="Black"/>
...
<Trigger Property="IsFocused" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontStyle" Value="Italic"/>
</Trigger>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I meant to say "When I click OUT of it"
On startup the DP is being set to the watermark, yet nothing appears in the TextBox
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
It's probably being overridden by the binding.
Changing the Text property to create a watermark isn't really a good idea. Try one of the other solutions from this SO thread[^], or this GitHub project[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I already saw the SO one. Not really a god idea either. It contains two textboxes and just hides either one.
The GitHub solution seems right.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I have a DataGrid bound to an ObservableCollection<myclass>. MyClass has a Name property on it. The model implements INotifyPropertyChanged.
When in the grid the user changes a Name value on any row the selected row'a Name property on the model is updated.
How can I know in ViewModel that a model bound to a data grid has changed?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I trap the Cell Edit End event with:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<telerik:RadTreeListView x:Name="tvData">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnded">
<i:InvokeCommandAction Command="{Binding TKCellEndEdit}" CommandParameter="{Binding CurrentCell,RelativeSource={RelativeSource AncestorType=telerik:RadTreeListView}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Viewmodel
public ICommand TKCellEndEdit { get; set; }
TKCellEndEdit = new DelegateCommand<object>(this.tKCellEndEdit);
private void tKCellEndEdit(object o)
{
string s = o.ToString();
GridViewCell oCell = (o as GridViewCell);
if (oCell.Value != null)
{
if (oCell.DataContext.GetType() == typeof(TreeNodeDE))
{
}
}
While that is for a telerik treelistview it is the same code for a gridview just changing the control.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
If the model implements INPC, your VM can hook into that and receive the notification when the Name changes. It doesn't need to get any more complicated than that.
This space for rent
|
|
|
|
|
I'm not sure I understand. Are you referring to listening to the INPC event in the VM? What would that look like?
Maybe something like this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
It's fairly straightforward to do. Hook up to the CollectionChanged event handler from your ObservableCollection and then, whenever the collection changes, you add or remove event handlers. The event handling looks a bit like this:
private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (object item in e.OldItems)
{
((INotifyPropertyChanged)item).PropertyChanged -= MyPropertyChangedHandler;
}
}
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
((INotifyPropertyChanged)item).PropertyChanged += MyPropertyChangedHandler;
}
}
} This means, whenever you add or remove an item to your collection, you now have the ability to hook into the models INPC chain. This might look a bit like this:
private void MyPropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != "Name") return;
MyModel model = sender as MyModel;
Debug.WriteLine($"Name changed to {model.Name}");
}
This space for rent
|
|
|
|
|
Ouch - I do that for a single item on the selectedobject property change, never considered it for an entire collection.
I almost never allow inline editing of grid data, the odd checkbox or single cell is handle by the celleditend event.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have to admit, I've never been the biggest fan of inline editing. I generally prefer to look at other methods of editing grid data, such as through the use of dialog boxes. Saying that, the approach I outlined here is simple enough to do (although I tend to use RX to watch collection and property changes).
This space for rent
|
|
|
|
|
Hi Guys, I have created a WPF Application which needs to run forever, so, I want to disable closing of application from task manager. I applied some code, and in apps section in task manager, I am able to disable closing of application, but when I close it from background processes in task manager then close it from apps section, it does not get restarted and get closed.
So, Is there any way by which I can restart my application once it gets closed from background processes in Task Manager or any other method to do this operation. Any help would be greatly appreciated. Please suggest.
|
|
|
|
|
About the only thing you could realistically do is have a service running that monitors the status of the application and then fires it off if it's been killed off. The service would have to periodically poll the application to determine whether or not the app was running (it would also have to take into account whether the app was closed because the computer was shutting down).
This space for rent
|
|
|
|
|
I have created a RadioButton who's style will look similar to this. In my version the line can be top, left, right, or bottom.
In the code behind I did:
public class MaroisRadioButton : RadioButton
{
public enum Position
{
Bottom,
Left,
Right,
Top
}
#region DP RadioMarkPosition
public static readonly DependencyProperty RadioMarkPositionProperty =
DependencyProperty.Register("RadioMarkPosition",
typeof(Position),
typeof(MaroisRadioButton),
new PropertyMetadata(Position.Left));
public Position RadioMarkPosition
{
get { return (Position)GetValue(RadioMarkPositionProperty); }
set { SetValue(RadioMarkPositionProperty, value); }
}
#endregion
#region DP RadioMarkSize
public static readonly DependencyProperty RadioMarkSizeProperty =
DependencyProperty.Register("RadioMarkSize",
typeof(double),
typeof(MaroisRadioButton),
new PropertyMetadata(null));
public double RadioMarkSize
{
get { return (double)GetValue(RadioMarkSizeProperty); }
set { SetValue(RadioMarkSizeProperty, value); }
}
#endregion
}
Then I created style for it and in the style I want to bind the RadioMarkSize to the Width property in the XAML:
<Border Grid.Row="1"
Grid.Column="0"
Margin="2"
Background="{DynamicResource buttonPressedBackBrush}"
Width="{Binding RadioMarkSize}"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
Name="LeftRadioMark"/>
But I can't get the binding to work. I get a binding error "BindingExpression path error: 'RadioMarkSize' property not found on 'object' ''MainWindowViewModel'"
How do I bind the Width property to the DP in the code behind?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
The DependencyProperty is on a derived RadioButton and you're attempting to read it on a Border bound to the VM DataContext. That's why your code can't see it. Why not put your derived RadioButton on there and get access to the DPs from that?
|
|
|
|