|
Find below a simple pulsing brush class I am using
public class PulsedBrush
{
public PulsedBrush()
{
Brush = new SolidColorBrush();
}
public PulsedBrush(Func<double, Color> get)
: this()
{
GetColor = get;
Update();
}
public Func<double, Color> GetColor { get; set; }
public SolidColorBrush Brush { get; private set; }
public static double DaySeconds
{
get
{
return DateTime.Now.TimeOfDay.Ticks / (double)TimeSpan.TicksPerSecond;
}
}
public void Update()
{
if (GetColor != null)
{
Brush.Color = GetColor(DaySeconds);
}
}
}
And simply call the Update() method on the CompositionTarget.Rendering event
|
|
|
|
|
I have a list of booleans, and I want to bind checkboxes to this list.\
Code Behind
private List<bool> _OnOffValues;
public List<bool> OnOffValues
{
get { return _OnOffValues; }
set
{
if (_OnOffValues != value)
{
_OnOffValues = value;
RaisePropertyChanged("OnOffValues");
}
}
}
public MyControl()
{
InitializeComponent();
this.DataContext = this;
OnOffValues = new List<bool>();
for (int x = 0; x < 24; x++)
{
OnOffValues.Add(true);
}
}
XAML
<CheckBox Grid.Row="0"
Grid.Column="0"
IsChecked="{Binding OnOffValues[0], ElementName=control}"/>
The output window shows
System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Boolean') from 'OnOffValues' (type 'List`1'). BindingExpression:Path=OnOffValues[0]; DataItem='MyControl' (Name='control'); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index'
The code in the contructor runs and the list is populated, but the output message appears BEFORE that.
What am I doing wrong here??
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Your binding assumes that OnOffValues is a dependency property of the control.
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
There's nothing there that says it's expecting a DP.
This space for rent
|
|
|
|
|
Look at his binding, IsChecked="{Binding OnOffValues[0], ElementName=control}" and the property, OnOffValues , is in the code behind of his user control. If he changes the property to a dependency property he won't get the error.
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
As explained below, it looks like he's using INPC so this is completely irrelevant.
This space for rent
|
|
|
|
|
Something like this,
Code behind
public partial class SomeControl : UserControl
{
public SomeControl()
{
InitializeComponent();
for (int i = 0; i < 20; i++)
{
OnOffValues.Add(true);
}
}
public List<bool> OnOffValues
{
get { return (List<bool>)GetValue(OnOffValuesProperty); }
set { SetValue(OnOffValuesProperty, value); }
}
public static readonly DependencyProperty OnOffValuesProperty =
DependencyProperty.Register("OnOffValues", typeof(List<bool>),
typeof(SomeControl), new PropertyMetadata(new List<bool>()));
}
XAML
<CheckBox IsChecked="{Binding Path=OnOffValues[0], RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
He's set the UserControl as its own DataContext. He doesn't need to create a DP if he's set the UC to implement INotifyPropertyChanged (which the property change notification suggests).
This space for rent
|
|
|
|
|
Quite true, I had taken note of that. I only supposed that the more apt approach would be to use a dependency property.
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
Not when he already set the DataContext. The issue was purely a timing issue and could be fixed with a FallbackValue.
This space for rent
|
|
|
|
|
If you check the timing of what's happening, the bind is happening as the control is being created - it happens in the InitializeComponent call in the user control. What you could do here is supply a FallbackValue so that the binding is satisfied until the point that your ViewModel is created.
This space for rent
|
|
|
|
|
Changing / setting the DataContext invokes the binding mechanism.
public MyControl()
{
OnOffValues = new List<bool>();
for (int x = 0; x < 24; x++)
{
OnOffValues.Add(true);
}
InitializeComponent();
this.DataContext = this;
}
And there is no "ElementName" in play here.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I have problem dragging file to the explorer.
It seems to be something to do with my current application settings.
I have a test app with hardcoded file and when the appropriate mouse event happen I trigger the following code (with hard coded file path)
var dragObj = new DataObject();
dragObj.SetFileDropList(new StringCollection() { @"H:\Temp\popeye.svg" });
DragDrop.DoDragDrop(new Window(), dragObj, DragDropEffects.Copy);
however if I do that in my other app (and I do it from the UI thread as well and with the same hard coded paths), nothing is dropped and the explorer show the forbidden cursor and the operation only complete when my mouse enter my window rect again.
I am stomped.. what could cause this code to work in one app, but not the other?
Remark: I am using the same version of the .NET Framework in both test and real app.
[EDIT]
Newsflash: this seems to be related to the UIElement I drag from being a drop target as well.
When I commented out the Drop event handlers, it dragged successfully.
Tricky, I need to be able to drop to it as well... mm....
[EDIT 2]
In my drop code I added some UIElement.MouseCapture() call. this was what prevented my code from working!
modified 21-Nov-18 11:06am.
|
|
|
|
|
I have a TreeView with EditableTextBlock in my HierarchicalDataTemplate for the TreeViewItems.
Now I have a focus problem.. when I navigate the TreeView with arrow key, the EditableTextBlock don't have Focus so I can't handle F2 (to start editing).
And also, I made my EditableTextBlock focusable, and now I have to press the directional arrow twice to change selected node with keyboard arrows...
How could I gracefully handle that, i.e.
1. arrow key down to change selected node
2. my EditableTextBlock has default an / or I can capture F2
[EDIT]
Nevermind, found a solution!
Added a property on my EditableTextBlock
FrameworkElement FocusableContainer
And listening to key event on this container.
modified 19-Nov-18 13:21pm.
|
|
|
|
|
Hi, I'm a bit late on WPF, I've developed c# Windows Forms for the last 10 years. Now I'm in the Xamarin world and I like the MVVM pattern.
I was thinking to move to WPF for Desktop developement (I need to support Windows 7).
I found myself very pleased to read the book Xamarin in action, I was looking for a great book on WPF.
Thanks.
Gassus.
|
|
|
|
|
WPF Recipes in C# 2008: A Problem-Solution Approach
(Don't let the release date throw you off).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Bonjour,
Lors de saisi une date erronée un cadre en rouge s'affiche en regroupant le textbox + l'icone de calendrier par contre je veux que le cadre entoure le textbox seulement.
Merci de consulter l'image ci-dessous.
https://i.postimg.cc/76mk2NFF/Capture1.jpg[^]
<datepicker>
<datepicker.selecteddate>
<binding path="DateDeb" stringformat="d" validatesondataerrors="True">
<binding.validationrules>
<communpresentation:regledatevalide>
<datepicker.resources>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<CommunPresentation:MaskedTextbox Mask="##/##/####" Text="{Binding Path=DateDeb}" />
</controltemplate>
</setter.Value>
</setter>
|
|
|
|
|
Please post questions in English.
S'il vous plaît poser des questions en anglais.
Google Translate: When typing a wrong date a red frame is displayed by grouping the textbox + the calendar icon by cons I want the frame surrounds the textbox only.
Please see the image below.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm creating a style for a button. This style creates a circle with an arrow in it. The problem is that when I mouse ever it the effect shows the button highlight.
Here's my code:
<Style x:Key="backButton" TargetType="{x:Type Button}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Grid.Column="0"
Grid.Row="0"
Canvas.Top="2"
Canvas.Left="2"
Fill="Transparent"
Height="35"
Width="35"
StrokeThickness="2"
Stroke="#6E6E6E"
Margin="2"
x:Name="part_ellipse"/>
<Path Grid.Column="0"
Grid.Row="0"
Margin="12,10,0,0"
Fill="#6E6E6E"
Data="M 0 10 L 10 0 L 10 20 Z"
x:Name="part_path"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="part_ellipse" Property="Fill" Value="#CCCCCC" />
<Setter TargetName="part_path" Property="Fill" Value="#CCCCCC" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
How do I remove the button's MouseOver effect?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
late reply.. you probably figure out the obvious by now but...
Remove that trigger!!!
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="part_ellipse" Property="Fill" Value="#CCCCCC" />
<Setter TargetName="part_path" Property="Fill" Value="#CCCCCC" />
</Trigger>
</ControlTemplate.Triggers>
|
|
|
|
|
Hello everyone,
I've tried to follow the steps described here: https://stackoverflow.com/questions/30751663/how-to-change-mahapps-metro-dialog-content-template-width
Here, I can't understand by which way is called "newly customized" modal-dialog window.
I've tried a way that was offered at mahapps home page. This way works and calls modal-dialog window as though there was nothing customized at all. In other words mahapps.modal window is called by default.
private async void button_Click(object sender, RoutedEventArgs e)
{
await this.ShowMessageAsync("Attention", "The product is out of range");
}
Any ideas please?
modified 24-Sep-18 13:22pm.
|
|
|
|
|
Still blocked?
I am using MahApp at home, I can have a look how I do things....
|
|
|
|
|
XAML
<DataGrid Grid.Row="1"
x:Name="dataGrid"
AutoGenerateColumns="False"
BorderBrush="SteelBlue"
BorderThickness="1"
Margin="5"/>
Code Behind
private ObservableCollection<DataGridColumn> _ColumnCollection;
public ObservableCollection<DataGridColumn> ColumnCollection
{
get { return _ColumnCollection; }
set
{
if (_ColumnCollection != value)
{
_ColumnCollection = value;
RaisePropertyChanged("ColumnCollection");
}
}
}
private DataTable _Datatable;
public DataTable Datatable
{
get { return _Datatable; }
set
{
if (_Datatable != value)
{
_Datatable = value;
RaisePropertyChanged("Datatable");
}
}
}
private void CreateGrid()
{
Datatable = new DataTable();
ColumnCollection = new ObservableCollection<DataGridColumn>();
Datatable.Columns.Add("Fruit", typeof(string));
Datatable.Rows.Add("Apples");
Datatable.Rows.Add("Oranges");
Datatable.Rows.Add("Grapes");
dataGrid.Columns.Add(new DataGridTextColumn()
{
Header = "Fruit Name",
Width = new DataGridLength(200),
FontSize = 12,
Binding = new Binding("Fruit")
});
var row = Datatable.Rows[0];
dataGrid.ItemsSource = Datatable.Rows;
}
I see the column called Fruit Name, and there are 3 data rows, yet I don't see the data.
The Output Window shows 6 lines like this:
System.Windows.Data Error: 40 : BindingExpression path error: 'Fruit' property not found on 'object' ''DataRow' (HashCode=51379511)'. BindingExpression:Path=Fruit; DataItem='DataRow' (HashCode=51379511); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
What am I doing wrong??
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Using "data tables" here buys you nothing except complexity; it's part of the ADO.NET "transport mechanism" (i.e. the "back end") that you are now muddying the front-end / view with.
Use you own collection of "objects" with meaningful property names (you're trying to bind to "column names" which are in fact "column keys").
And since you control the "data source" (elements), you can also control the columns that are "auto generated" (and there is no need to add them manually in that case).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
XAML
<DataGrid Grid.Row="1"
x:Name="dataGrid"
AutoGenerateColumns="False"
BorderBrush="SteelBlue"
BorderThickness="1"
Margin="5"/>
Code Behind
private ObservableCollection<DataGridColumn> _ColumnCollection;
public ObservableCollection<DataGridColumn> ColumnCollection
{
get { return _ColumnCollection; }
set
{
if (_ColumnCollection != value)
{
_ColumnCollection = value;
RaisePropertyChanged("ColumnCollection");
}
}
}
private DataTable _Datatable;
public DataTable Datatable
{
get { return _Datatable; }
set
{
if (_Datatable != value)
{
_Datatable = value;
RaisePropertyChanged("Datatable");
}
}
}
private void CreateGrid()
{
Datatable = new DataTable();
ColumnCollection = new ObservableCollection<DataGridColumn>();
Datatable.Columns.Add("Fruit", typeof(string));
Datatable.Rows.Add("Apples");
Datatable.Rows.Add("Oranges");
Datatable.Rows.Add("Grapes");
dataGrid.Columns.Add(new DataGridTextColumn()
{
Header = "Fruit Name",
Width = new DataGridLength(200),
FontSize = 12,
Binding = new Binding("Fruit")
});
var row = Datatable.Rows[0];
dataGrid.ItemsSource = Datatable.Rows;
}
I see the column called Fruit Name, and there are 3 data rows, yet I don't see the data.
The Output Window shows 6 lines like this:
System.Windows.Data Error: 40 : BindingExpression path error: 'Fruit' property not found on 'object' ''DataRow' (HashCode=51379511)'. BindingExpression:Path=Fruit; DataItem='DataRow' (HashCode=51379511); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
What am I doing wrong??
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|