I'd like to dynamically create a Slider with its value bind to TextBox inside the DockPanel. When I'm trying to do so I cannot bind the slider value to the TextBox, and inside the TextBox I'm getting the following message: {Binding ElementName=slValue, Path=Value, UpdateSourceTrigger=PropertyChanged} instead of the Slider's value.
Here's the code I've written so far:
double minimum = 0.0;
double maximum = 100.0;
double defaultValue = 5.0;
DockPanel item = new DockPanel();
item.VerticalAlignment = VerticalAlignment.Center;
Slider slValue = new Slider()
{
Minimum = minimum,
Maximum = maximum,
TickFrequency = 1.0,
Value = defaultValue,
IsSnapToTickEnabled = true,
Name = "slValue",
Width = 100
};
TextBox slValueTB = new TextBox()
{
Text = "{Binding ElementName=slValue, Path=Value, UpdateSourceTrigger=PropertyChanged}",
TextAlignment = TextAlignment.Right,
Width = 40,
};
item.Children.Add(slValue);
item.Children.Add(slValueTB);
And here's the xml code I'm trying to recreate dynamically:
<DockPanel VerticalAlignment="Center" Margin="10">
<TextBox Text="{Binding ElementName=slValue, Path=Value, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" TextAlignment="Right" Width="40" />
<Slider Minimum="0" Maximum="100" TickPlacement="BottomRight" TickFrequency="5" IsSnapToTickEnabled="True" Name="slValue" />
</DockPanel>
{Binding ...}
is a markup extension that only works in XAML, not in code behind. See How to: Create a Binding in Code. – Clemens