C# & XAML - TextBox binding not updating content - c#

I have a TextBox in a Stackpanel, as in the code below
<StackPanel x:Name="EtenStack" Visibility="{Binding Path=Sort, ConverterParameter=Eten, Converter={StaticResource convertEten}}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label Content="Bereidingstijd"/>
<TextBox Height="23" Width="150" Text="{Binding Path=Time, TargetNullValue='', Mode=TwoWay}"/>
</StackPanel>
When the Visibility is set to Visible in my converter, my textbox doesn't update it's Text property, even though the Property gets it's correct value (tested by showing a MessageBox with the Property).
Any thoughts?

The Time property will need to be either a Dependency Property with the right binding or on a class that Implements the INotifyPropertyChanged Interface, for the Time property, in order for the update to occur "Automatically."

Related

WPF -How to access the fields present in a template

Let me explain so I have a wpf application I used a listBox with a template. This template contains a TextBlock and a ComboBox. When running the application, everything goes well, my list is initialized correctly. But then I would like to retrieve the values ​​of my TextBlock and my comboBox and I don't understand how I can achieve this.
I am attaching the part of my XAML code that deals with this listBox :
<ListBox x:Name="ListBoxEnv" Grid.Row="1"
d:ItemsSource="{d:SampleData ItemCount=5}" Width="460"
SelectionChanged="ListBoxEnv_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="TxtBlockEnv"
VerticalAlignment="Center" HorizontalAlignment="Left"
Text="{Binding EnvName}"/>
<ComboBox x:Name="ComboBoxEnv"
VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding EnvListValue}"
Margin="100,2,0,2" Width="200"
SelectionChanged="ComboBoxEnv_SelectionChanged"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The EnvName property is readonly because it is bound to TextBlock, so you can ignore it. Change binding to: Text="{Binding EnvName, Mode=OneTime}" to save the app resources.
However to extract selected environment in every combo in the list you need to add to ComboBox template: SelectedItem={Binding SelectedEnv} and add new property to SampleData
for example
public MyEnvironmentClass SelectedEnv {get; set;}

How to create only binding not null controls in xaml?

I`d like select between combobox and textbox depending on ViewModel. In the view model i have two properties one of them should be null and the other not null.
enter image description here
I´m not sure if that´s exactly what you want to do. As CarbineCoder already said, some more code would be helpful. If you want to show only one of both controls you could create two properties called for example tbVisibility and cbVisibility. If you want to show the ComboBox, then cbVisibility has to be "visible" and tbVisibility has to be "hidden".
XAML would like that:
<StackPanel Orientation="Horizontal">
<!--This is visible if tbVisibility is "visible" and cbVisibility is "hidden"-->
<Label Width="100" Content="label" Visibility="{Binding tbVisibility}"></Label>
<TextBox Width="184" Text="Textbox" Visibility="{Binding tbVisibility}"></TextBox>
<!--This part is visible if cbVisibility is "visible" and tbVisibility is "hidden"-->
<Label Width="100" Content="label" Visibility="{Binding cbVisibility}"></Label>
<TextBox Width="184" Visibility="{Binding cbVisibility}"></TextBox>
</StackPanel>
You only have to define in you code which property is visibile and which is not.

Textblock modify text automatically

Let's say I have two TextBlock on the same page and both binding to the same variable:
<TextBlock Text={Binding [someViewModel].someText}/>
<TextBlock Text={Binding [someViewModel].someText}/>
//someText = "testingText"
I'm wondering if it's possible to have the first textblock to show "tes......" and the second textblock showing "testingText"?
Try this
<StackPanel>
<TextBlock Text="testingtext" MaxWidth="20" TextTrimming="CharacterEllipsis" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="testingtext" ></TextBlock>
</StackPanel>
output
Make an IValueConverter for one of the bindings which converts the String to the custom format you want.

Silverlight: ComboBox Behavior

really simple problem, but I guess I have just the wrong definition of combobox:
I'd like to get a simple thing like:
http://www.c-sharpcorner.com/uploadfile/mahesh/combobox-in-silverlight/
But whenever I add a combobox (or a listbox) and set the itemssource, it shows directly all items and I dont have a textbox-like selection.
My approach was quite simple:
In XAML I define:
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Style="{StaticResource styleStdWidth}" Text="Spieler 1:" />
<ListBox x:Name="lsbPlayerOne" ItemTemplate="{StaticResource dtName}" Width="300" />
<TextBox x:Name="txtPlayerOnePoints" Style="{StaticResource stylePlayerWidth}" />
</StackPanel>
<DataTemplate x:Name="dtName">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" FontSize="35" FontWeight="Bold" x:Name="txbname"/>
</StackPanel>
</DataTemplate>
And in Code behind I just set the ItemsSource with a List, which has data.
Since the ListBox gets bigger every time I add a item, it gets uglier and uglier.
Am I missing a property, which I didnt find? I did not see anything...
Sorry for the confusing question :)
P.S.: I tried the same as in the example shown in the link. Sadly I cant open the sample project.
Matthias Müller
Your question is unclear. But you are not implementing a combobox in the code you have shown. Why don't you use a combobox and set your itemsource to the list that contains the fields you want to use?
<ComboBox ItemSource={Binding Names}/>

Data bound WPF ComboBox with choices defined in XAML?

On my viewmodel I've got an int property and I want to expose it for editing with a ComboBox, with a limited set of choices, such as 16, 8, 4 and 2. Is there a way to specify the choices in the XAML, while still binding the value back to the viewmodel? I'd want to do something like this:
<ComboBox SelectedValue="{Binding MyIntProperty}">
<ComboBoxItem>16</ComboBoxItem>
<ComboBoxItem>8</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
</ComboBox>
I know I could rig up a List<int> in code and set that as the ItemsSource, but I'm hoping there's a way to do this that doesn't involve an extra property in the viewmodel that exposes a collection created in code.
You can specify your choices exactly as you are in your example. What it looks like your missing, to make it work, is the SelectedValuePath property. Without it, the SelectedValue would be the same as the SelectedItem. By setting SelectedValuePath="Content" in the ComboBox you can specify that your SelectedValue binding is instead binding to just a portion of the SelectedItem, in this case the Int content you specified as the content in each ComboBoxItem.
Here's a small demo with it, and also binding the value to a TextBox, where you can set the item and see it reflected in the ComboBox through the SelectedValue binding (or vice versa).
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Set Value:" />
<TextBox Text="{Binding MyIntProperty, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Select Value:" />
<ComboBox SelectedValue="{Binding MyIntProperty}" SelectedValuePath="Content">
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>6</ComboBoxItem>
<ComboBoxItem>8</ComboBoxItem>
<ComboBoxItem>16</ComboBoxItem>
</ComboBox>
</StackPanel>
</StackPanel>

Categories

Resources