Why there is no TemplatePartAttribute in ComboBox?
UWP:
[Composable(typeof(IComboBoxFactory), CompositionType.Public, 65536, "Windows.Foundation.UniversalApiContract")]
[ContractVersion(typeof(UniversalApiContract), 65536)]
[MarshalingBehavior(MarshalingType.Agile)]
[Static(typeof(IComboBoxStatics2), 65536, "Windows.Foundation.UniversalApiContract")]
[Static(typeof(IComboBoxStatics), 65536, "Windows.Foundation.UniversalApiContract")]
[Threading(ThreadingModel.Both)]
[WebHostHidden]
public class ComboBox : Selector, IComboBox, IComboBoxOverrides, IComboBox2
WPF:
[Localizability(LocalizationCategory.ComboBox)]
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(ComboBoxItem))]
[TemplatePart(Name = "PART_EditableTextBox", Type = typeof(TextBox))]
[TemplatePart(Name = "PART_Popup", Type = typeof(Popup))]
public class ComboBox : Selector
Related
I'd like to get value from Binding in my code and use it as a string. How Can I do that?
Binding b = new Binding("MyProperty")
{
Source = myobject
};
//[...]
string value = b //HOW TO GET VALUE FROM b ?
BTW:
I would like the Converter attached to Binding to be called when retrieving this value.
I found that the solution could be an auxiliary class with DependencyProperty.
Auxiliary class
public class TestClass : FrameworkElement
{
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(TestClass), new PropertyMetadata(""));
}
Binding conversion to String
Binding b = new Binding("MyProperty") { Source = myobject };
TestClass tc = new TestClass { DataContext = b };
BindingOperations.SetBinding(tc, TestClass.MyPropertyProperty, b);
string txt = tc.MyProperty;
Advantages:
You can use Binding and MultiBinding
You can use Converters
Disadvantages:
Each time we create a class that inherits from the FrameworkElement, which means that we do unnecessary operations.
I'm trying change the line thickness in a serie dinamically created, I need turn the line more thick.
Below, follow the code to bind the created serie on chart component. It works fine, but I tryed adapt this in this code and I had no sucess.
Please help, thanks.
Style style = new Style(typeof(LineDataPoint));
style.Setters.Add(new Setter(LineDataPoint.OpacityProperty, (double)(0.0)));
style.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, dadosSerie.ColorSerie));
LineSeries lineSerie = new LineSeries()
{
Title = dadosSerie.SerieTitle,
IndependentValueBinding = new Binding("Key"),
DependentValueBinding = new Binding("Value"),
DependentRangeAxis = dadosSerie.EixoY,
DataPointStyle = style,
ItemsSource = dadosSerie.DataSerie,
};
chtGraficos.Series.Add(lineSerie);
Have you tried adding a Style for the serie's Polyline instead?
It seams the style for the LineDataPoint is actually for every point on the serie.
Here is a working sample of a chart fully created on code-behind. You just have to create a window named MainWindow and add a reference on the project to System.Windows.Controls.DataVisualization.Toolkit:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var valueList = new Dictionary<string, int>();
valueList.Add("Developer", 60);
valueList.Add("Misc", 20);
valueList.Add("Project Manager", 40);
var style = new Style(typeof(Polyline));
style.Setters.Add(new Setter(Polyline.StrokeThicknessProperty, 10d));
var series = new LineSeries
{
PolylineStyle = style,
ItemsSource = valueList,
DependentValuePath = "Value",
IndependentValuePath = "Key",
};
var lineChart = new Chart { Height = 254 };
lineChart.Series.Add(series);
var mainGrid = new Grid();
mainGrid.Children.Add(lineChart);
this.Content = mainGrid;
}
}
I don't want to use MVVM and want to change the selected row Foreground on my datagrid in code behind (in SelectionChanged EventHandler function), but I can't find the solid way.
My row can be black, blue and red, but shows the color with a higher priority based on some condition. After selecting the current row I should remove, f.e. black color from my priority list.
I have some class:
public class TempClass{ public string cell1 { get; set; }; public string cell2 { get; set; };}
and
TempClass[] collection;
bound with my datagrid:
datagrid.ItemsSource = collection;
Any idea?
var rowStyle = new Style {TargetType = typeof (DataGridRow)};
rowStyle.Setters.Add(new Setter(ForegroundProperty, Brushes.Green));
var rowTrigger = new Trigger {Property = DataGridRow.IsSelectedProperty, Value = true};
rowTrigger.Setters.Add(new Setter(ForegroundProperty, Brushes.Red));
rowTrigger.Setters.Add(new Setter(BackgroundProperty, Brushes.Orange));
rowStyle.Triggers.Add(rowTrigger);
var cellStyle = new Style {TargetType = typeof (DataGridCell)};
var cellTrigger = new Trigger {Property = DataGridCell.IsSelectedProperty, Value = true};
cellTrigger.Setters.Add(new Setter(ForegroundProperty, Brushes.Red));
cellTrigger.Setters.Add(new Setter(BackgroundProperty, Brushes.Orange));
cellStyle.Triggers.Add(cellTrigger);
datagrid.RowStyle = rowStyle;
datagrid.CellStyle = cellStyle;
I am trying to create a DataGrid which is populated by setting the ItemsSource property to an ObservableCollection of PropertyGroup objects where each PropertyGroup object contains an ObservableCollection of Property Objects. All the PropertyGroups have the same number of Property Objects, and so I am binding to them via a path using and array subscript. Every thing works fine, except that I get the following binding error AFTER I remove a PropertyGroup object from the DataGrid.
System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'PropertyElement')
from 'Children' (type 'ObservableCollection`1'). BindingExpression:Path=Children[3]
.Value; DataItem='PropertyGroupImpl' (HashCode=23661558); target element
is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException:
Specified argument was out of the range of valid values.
Parameter name: index'
My code:
public class DataGridView : UserControl
{
public DataGridView()
{
Rows = new ObservableCollection<PropertyGroup>();
m_DataGrid = new DataGrid();
m_DataGrid.AutoGenerateColumns = false;
m_DataGrid.ItemsSource = Rows;
Content = m_DataGrid;
}
public ObservableCollection<PropertyGroup> Rows { get; set; }
public void AddRowGroup(PropertyGroup propertyGroup)
{
if(Rows.Count == 0)
InitDataGrid(propertyGroup);
Rows.Add(propertyGroup);
}
public void RemoveRowGroup(PropertyGroup propertyGroup)
{
Rows.Remove(propertyGroup);
}
void InitDataGrid(PropertyGroup firstGroup)
{
for(int i = 0; i < firstGroup.Children.Count; ++i)
{
Property prop = firstGroup.Children[i] as Property;
DataGridColumn dgCol = null;
Binding bnd = new Binding();
bnd.Path = new PropertyPath("Children[" + i + "].Value");
if(prop.Type == Property.EnumType.eBool)
dgCol = CreateBooleanColumn(bnd);
else
dgCol = CreateTextColumn(bnd, prop.Value.GetType());
dgCol.Header = prop.Name;
m_DataGrid.Columns.Add(dgCol);
}
}
DataGridColumn CreateTextColumn(Binding bnd, Type propType)
{
var textCol = new DataGridTextColumn();
// Styling code removed for brevity
textCol.Binding = bnd;
return textCol;
}
DataGrid m_DataGrid;
DataGridColumn CreateBooleanColumn(Binding bnd)
{
var chkBoxCol = new DataGridCheckBoxColumn();
chkBoxCol.Binding = bnd;
return chkBoxCol;
}
}
public class PropertyGroup
{
public PropertyGroup()
{
Children = new ObservableCollection<PropertyElement>();
}
public ObservableCollection<PropertyElement> Children { get; set; }
}
public class Property : PropertyElement
{
public enum EnumType {eBool, eInt, eUInt, eFloat, eDouble, eString,
eVector2, eVector3, eVector4, eEnum};
public EnumType Type { get; set; }
public object Value { get; set; }
}
public class PropertyElement
{
public string Name { get; set; }
}
The binding error occurs after RemoveRowGroup() is called for a PropertyGroup when the child Property objects are being removed from the PropertyGroup's Children ObservableCollection.
It seems as though the BindingExpressions binding the DataGrid's cells to Property.Value are still trying to update after the object has been removed from the DataGrid.
Any ideas?
How about assign the Source property without index in PropertyPath?
Binding bnd = new Binding();
bnd.Path = new PropertyPath("Children[" + i + "].Value");
=>
Binding bnd = new Binding();
bnd.Source = firstGroup.Children[i];
bnd.Path = new PropertyPath("Value");
PS: I'm not good at english
This is similar to my previous question, but that solution did not solve this problem.
fontSizeProperty is not being recognized when I move a method from my Silverlight MainPage code behind (which worked) to a new class in a silverlight library
using System.Windows.Controls;
namespace MyNameSpace
{
public static class DataGridBuilder
{
private static Style BuildHeaderStyle(string tooltip)
{
Style newGridHeaderStyle = new Style(typeof(DataGridColumnHeader));
newGridHeaderStyle.Setters.Add(new Setter { Property = FontSizeProperty, Value = 9.0 });
newGridHeaderStyle.Setters.Add(new Setter { Property = FontWeightProperty, Value = FontWeights.Bold });
return newGridHeaderStyle;
}
}
}
NOTE: Per MSDN for FontSizeProperty, I do include System.Windows reference, and "using System.Windows.Control"
Based on answers below, I changed "Property = FontSizeProperty" to "Property=DataGridColumnHeader.FontSizeProperty" etc., like this:
private static Style BuildHeaderStyle(string tooltip)
{
FontWeight fw = FontWeights.Bold;
Style newGridHeaderStyle = new Style(typeof(DataGridColumnHeader));
newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.FontSizeProperty, Value = 9.0 });
newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.FontWeightProperty, Value = FontWeights.Bold });
newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.ContentTemplateProperty, Value = CreateDataGridHeaderTemplate(tooltip) });
return newGridHeaderStyle;
}
I believe you want Control.FontSizeProperty and Control.FontWeightProperty instead.
Your MainPage is a user control, which has Control as a superclass and hence inherits the above two dependency properties. Your static class isn't a subclass of Control so it doesn't inherit these dependency properties.
FontSizeProperty is defined on Control, which you do not derive from, so you have to use Control.FontSizeProperty.