I have a UserControl where I'd like to have some parameters of customization "radius"(double) and "contentSource" (string[]).
My UserControl is composed of a few nested controls:
<UserControl ...>
<Grid>
<my:Menu ...>
<my:Button>
</my:Button>
<my:Button>
</my:Button>
<my:Button>
</my:Button>
</my:Menu ...>
</Grid>
I'm trying to expose the parameters with:
public double Rad
{
get { return (double)GetValue(RadProperty); }
set { SetValue(RadProperty, value); }
}
public static readonly DependencyProperty RadProperty =
DependencyProperty.Register(
"Radius",
typeof(double),
typeof(Menu));
public String[] DataSource
{
get { return (String[])GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register(
"DataSource",
typeof(String[]),
typeof(Menu));
However, there seems to be two problems, the "string[]" parameter seems to be causing crashes, but mostly, I cannot set the "Radius" property at all. Is there something else I need to do to expose the parameter?
How are you trying to access the values? I have copied your code into a UserControl and it seems to work fine. Have you set the DataContext for the object you are accessing these values from?
Here is my test code, which might help:
public partial class uc : UserControl
{
public uc()
{
InitializeComponent();
this.DataContext = this;
this.DataSource = new string[] { "hello","There" };
this.Rad = 7;
}
public String[] DataSource
{
get { return (String[])GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register(
"DataSource",
typeof(String[]),
typeof(uc));
public double Rad
{
get { return (double)GetValue(RadProperty); }
set { SetValue(RadProperty, value); }
}
public static readonly DependencyProperty RadProperty =
DependencyProperty.Register(
"Radius",
typeof(double),
typeof(uc));
}
and the XAML:
<UserControl x:Class="WpfApplication18.uc"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel>
<TextBox Text="{Binding Path=DataSource[0]}"></TextBox>
<TextBox Text="{Binding Path=DataSource[1]}"></TextBox>
<TextBox Text="{Binding Path=Radius}"></TextBox>
<TextBox Text="{Binding Path=Radius}"></TextBox>
</StackPanel>
</Grid>
</UserControl>
Related
I am trying to store some data in XAML and loading it at runtime. For storing my xaml look like this .
<osp:OSPData x:Class="OptisetStore.Model.OSPData"
xmlns:osp="clr-namespace:OptisetStore.Model;assembly=OptisetStore"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OptisetStore.Model"
mc:Ignorable="d"
osp:OSPData.Name="OspCollection">
<osp:OSPData.Features>
<osp:Feature LocalName="feature1" x:Name="F1" IsEnabled="True" />
<osp:Feature LocalName="{Binding ElementName=F1, Path=LocalName, Mode=TwoWay}" IsEnabled="{Binding ElementName=F1, Path=IsEnabled, Mode=TwoWay}" />
</osp:OSPData.Features>
OSPData class
public partial class OSPData : DependencyObject
{
public string Name { get; set; }
public OSPData()
{
Features = new ObservableCollection<Feature>();
}
public ObservableCollection<Feature> Features
{
get { return (ObservableCollection<Feature>)GetValue(FeaturesProperty); }
set { SetValue(FeaturesProperty, value); }
}
// Using a DependencyProperty as the backing store for Features. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FeaturesProperty =
DependencyProperty.Register("Features", typeof(ObservableCollection<Feature>), typeof(OSPData), new PropertyMetadata(new ObservableCollection<Feature>()));
}
Feature Class:
public class Feature : DependencyObject
{
public bool IsEnabled
{
get { return (bool)GetValue(IsEnabledProperty); }
set { SetValue(IsEnabledProperty, value); }
}
// Using a DependencyProperty as the backing store for IsEnabled. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.Register("IsEnabled", typeof(bool), typeof(Feature), new PropertyMetadata(false));
public string LocalName
{
get { return (string)GetValue(LocalNameProperty); }
set { SetValue(LocalNameProperty, value); }
}
// Using a DependencyProperty as the backing store for LocalName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LocalNameProperty =
DependencyProperty.Register("LocalName", typeof(string), typeof(Feature), new PropertyMetadata(""));
}
So after my ospdata is ready I store and at runtime I load the class to populate my UI. but Element name binding is not working.
StringReader stringReader = new StringReader(File.ReadAllText("Model/OSPData.xaml"));
XmlReader xmlReader = XmlReader.Create(stringReader);
var data = (OSPData)XamlReader.Load(xmlReader);
SimpleIoc.Default.GetInstance<TestingViewModel>().Data = data;
and my ui look like this :
<UniformGrid>
<ListBox ItemsSource="{Binding Data.Features}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding LocalName}" Width="100" />
<CheckBox Content="IsEnabled" IsChecked="{Binding IsEnabled}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UniformGrid>
I want the ability to edit OSPData.xaml and push it to application at runtime.
I have a custom usercontrol with that code:
public partial class AudioControl : UserControl
{
public AudioControl()
{
InitializeComponent();
Value = 0.1;
DataContext = this;
}
public event RoutedPropertyChangedEventHandler<double> ValueChanged = delegate { };
public int TextWidth
{
get { return (int)GetValue(TextWidthProperty); }
set { SetValue(TextWidthProperty, value); }
}
public int SliderWidth
{
get { return (int)GetValue(SliderWidthProperty); }
set { SetValue(SliderWidthProperty, value); }
}
public string Header
{
get { return (string)GetValue(TextBlock.TextProperty); }
set { SetValue(TextBlock.TextProperty, value); }
}
//public double Value
//{
// get { return (double)GetValue(Slider.ValueProperty); }
// set { SetValue(Slider.ValueProperty, value); }
//}
public double Value
{
get {
return (double)GetValue(ValueProperty);
}
set {
SetValue(ValueProperty, value);
}
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(AudioControl), new UIPropertyMetadata(0));
public static readonly DependencyProperty TextWidthProperty =
DependencyProperty.Register("TextWidth", typeof(int), typeof(AudioControl), new UIPropertyMetadata(0));
public static readonly DependencyProperty SliderWidthProperty =
DependencyProperty.Register("SliderWidth", typeof(int), typeof(AudioControl), new UIPropertyMetadata(0));
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ValueChanged(this, e);
}
}
And this XAML:
<UserControl x:Class="Controller.Audio.AudioControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Audio="clr-namespace:Controller.Audio"
mc:Ignorable="d"
d:DesignHeight="23" d:DesignWidth="500" x:Name="audioControl">
<UserControl.Resources>
<Audio:DoubleToIntConverter x:Key="doubleToInt"/>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding TextWidth}"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="{Binding SliderWidth}"/>
</Grid.ColumnDefinitions>
<Label x:Name="txtBlock" Margin="0,0,10,0">
<Binding Path="Header"/>
</Label>
<Label Grid.Column="1" Content="{Binding ElementName=slider, Path=Value, Converter={StaticResource doubleToInt}}"/>
<Slider x:Name="slider" Grid.Column="2" Style="{StaticResource Office2010SilverSliderStyle}"
Value="{Binding Path=Value, ElementName=audioControl}" Minimum="0.0" Maximum="1.0" LargeChange="0.25" TickFrequency="0.01" ValueChanged="Slider_ValueChanged"/>
</Grid>
So if i start it I get an exception that something with the value binding is wrong. I also tried the commented version (the value-Property). There is no exception but if i set the value of that property my slider does not change.
Does anyone has any idea why? I ve never done something like that :(
There's an error in the following dependency property declaration:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(AudioControl), new UIPropertyMetadata(0));
The problem here is that you're defining the dependency property to be of type double, but giving it a default value 0 that is an int. Try changing 0 to 0.0.
I ran your code and encountered an exception. The innermost exception contained the following message:
Default value type does not match type of property 'Value'.
I changed the 0 in the line above to 0.0 and the problem went away.
I have a usercontrol which has a couple of textblocks on it
<UserControl x:Class="Tester.Messenger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="myUserControl"
>
<TextBlock Text="{Binding ElementName=myUserControl,Path=Header,Mode=TwoWay}" Foreground="LightGray" FontSize="11" Margin="3,3,0,-3"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding ElementName=myUserControl,Path=Message, Mode=TwoWay}" Foreground="White" FontSize="16" Margin="3,-5"/>
In my code behind I have two dependency properties that I'm binding the above textblocks Text property to.
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("HeaderProperty", typeof(string), typeof(UserControl), new PropertyMetadata("header"));
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("MessageProperty", typeof(string), typeof(UserControl), new PropertyMetadata(null));
public string Header
{
get
{
return (string)GetValue(HeaderProperty);
}
set
{
SetValue(HeaderProperty, value);
}
}
public string Message
{
get
{
return (string)GetValue(MessageProperty);
}
set
{
SetValue(MessageProperty, value);
}
}
When I create a object of my UserControl and I change the Header and Message properties and place the control in an ItemControls items collection, then these aren't being reflected in the control. The control just displays the default values for the Header and Message.
Messenger m = new Messenger();
m.Header = "colin";
m.Message = "Download File ?";
iControl.Items.Add(m);
The first parameter in your call to DependencyProperty.Register is incorrect:
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("HeaderProperty", typeof(string), typeof(UserControl), new PropertyMetadata("header"));
should be:
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(string), typeof(UserControl), new PropertyMetadata("header"));
The string should be the name of the property as it will appear in XAML, that is without the "Property" suffix. Same goes for your message DependencyProperty.
today I getting crazy while trying to do, what I think, is simple thing.
I want to be able to create my usercontrol, and use it in my column template in my datagrid
I have searched and tried several combinations, and nothing appear to work
Can anyone help me?
public class User
{
public string Name { get; set; }
public bool IsValid { get; set; }
}
partial class MyControl : UserControl
{
private string _value;
public string Value
{
get { return _value; }
set { _value = value;
txt.Text = value;
}
}
public MyControl()
{
InitializeComponent();
}
}
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="txt" Text="[undefined]"></TextBlock>
</Grid>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
var items = new List<User>();
items.Add(new User{Name = "user 1", IsValid = true});
items.Add(new User { Name = "user 2", IsValid = false });
myGrid.ItemsSource = items;
}
}
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid x:Name="myGrid" AutoGenerateColumns="False" IsReadOnly="True">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Name">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<SilverlightApplication1:MyControl Value="{Binding Name}"></SilverlightApplication1:MyControl>
<!--<TextBlock Text="{Binding Name}"></TextBlock>-->
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
Edited:
I also tried the following, but I get no results on my grid:
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="txt" Text="{Binding Value}"></TextBlock>
</Grid>
public partial class MyControl : UserControl
{
public DependencyProperty ValueProperty =
DependencyProperty.Register("Value",
typeof(string),
typeof(MyControl),
new PropertyMetadata(OnValueChanged));
public string Value
{
get
{
return (string)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
NotifyPropertyChanged("Value");
}
}
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MyControl) d).Value = (String)e.NewValue; //ERROR: here I got always empty string
}
public MyControl()
{
InitializeComponent();
}
}
The reason why your first code didn't work is simple. To be able to bind the "Value" property on your "MyControl" (Value={Binding Name}), it has to be a Dependency Property. which you fixed in your second bit of code.
Here's what I did (and that worked well):
<UserControl x:Class="BusinessApplication8_SOF_Sandbox.Controls.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" Name="myControl">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Name="textBlock" Text="{Binding Value, ElementName=myControl}"/>
</Grid>
</UserControl>
For the rest, I used your code.
Another possibility, which should be OK in case you only want the data to flow in one direction ("One Way" from source to target), as it is the case when using the TextBlock control is to update the Text property in the "OnValueChanged". here's the code for the Value property:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(string), typeof(MyControl),
new PropertyMetadata("", OnValueChanged));
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (MyControl)d;
var oldValue = (string)e.OldValue;
var newValue = target.Value;
target.OnValueChanged(oldValue, newValue);
}
protected virtual void OnValueChanged(string oldValue, string newValue)
{
textBlock.Text = newValue;
}
and you can remove the binding in xaml:
<TextBlock Name="textBlock" />
this worked for me as well.
Hope this helps ;)
you need to implement the INotifyPropertyChanged interface in your User class so that bound user controls are aware if any of the bound properties change. See the following page with the details how to implement it : http://www.silverlightshow.net/tips/How-to-implement-INotifyPropertyChanged-interface.aspx
As you can see you need to implement the interface and in the setters raise the event OnPropertyChanged
Then it should work with your bindings.
Best,
Tim
I'm trying to create a user control with dependency properties to bind to. Internally I have a ComboBox that is bound to these same properties, but the binding only works one way. The ComboBox fills from the ItemsSource, but SelectedItem doesn't get updated back to the viewmodel I'm binding to.
A simplified example:
This is the view model to bind with the user control:
public class PeopleViewModel : INotifyPropertyChanged
{
public PeopleViewModel()
{
People = new List<string>( new [] {"John", "Alfred","Dave"});
SelectedPerson = People.FirstOrDefault();
}
public event PropertyChangedEventHandler PropertyChanged;
private IEnumerable<string> _people;
public IEnumerable<string> People
{
get { return _people; }
set
{
_people = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("People"));
}
}
}
private string _selectedPerson;
public string SelectedPerson
{
get { return _selectedPerson; }
set
{
_selectedPerson = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("SelectedPerson"));
}
}
}
}
This is the User control:
<UserControl x:Class="PeopleControlTest.PeopleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="56" d:DesignWidth="637">
<StackPanel >
<ComboBox Margin="11"
ItemsSource="{Binding BoundPeople, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding BoundSelectedPerson, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</StackPanel>
with code behind
public partial class PeopleControl : UserControl
{
public PeopleControl()
{
InitializeComponent();
}
public static readonly DependencyProperty BoundPeopleProperty =
DependencyProperty.Register("BoundPeople", typeof(IEnumerable<string>), typeof(PeopleControl), new UIPropertyMetadata(null));
public static readonly DependencyProperty BoundSelectedPersonProperty =
DependencyProperty.Register("BoundSelectedPerson", typeof(string), typeof(PeopleControl), new UIPropertyMetadata(""));
public IEnumerable<string> BoundPeople
{
get { return (IEnumerable<string>)GetValue(BoundPeopleProperty); }
set { SetValue(BoundPeopleProperty, value); }
}
public string BoundSelectedPerson
{
get { return (string)GetValue(BoundSelectedPersonProperty); }
set { SetValue(BoundSelectedPersonProperty, value); }
}
}
And this is how I bind the user control in the main window (with the windows data context set to an instance of the viewmodel)
<Window x:Class="PeopleControlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:PeopleControlTest"
Title="MainWindow" Height="350" Width="525">
<Grid>
<controls:PeopleControl
BoundPeople="{Binding People}"
BoundSelectedPerson="{Binding SelectedPerson}"/>
</Grid>
</Window>
The combobox in the user control fills with the names, but when I select a different name this doesn't get updated back to the view model. Any idea what I'm missing here?
Thanks!
Some properties bind two-way by default (Including SelectedItem) but your BoundSelectedPerson does not. You can set the Mode of the binding:
<controls:PeopleControl
BoundPeople="{Binding People}"
BoundSelectedPerson="{Binding SelectedPerson, Mode=TwoWay}"/>
Or you can make it TwoWay by default by setting a flag on the DependencyProperty:
public static readonly DependencyProperty BoundSelectedPersonProperty =
DependencyProperty.Register("BoundSelectedPerson", typeof(string), typeof(PeopleControl), new FrameworkPropertyMetadata("",FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));