I had bind my textblock in xaml, is it possible to get the value out into my coding?
My coding for binding
<TextBlock Height="40" HorizontalAlignment="Left" Margin="8,24,10,0" Name="txtBlockCustName" Text="{Binding CustName, Mode=OneWay}" VerticalAlignment="Top" FontSize="26" />
I want to put in my mainpage.xaml.cs like
string CustName = txtBlockCustName.Text;
but it had error on it..
You can't access this textblock because it is bound in a listboxtemplate. If there are multiple textblocks in the list, how can you access it by name? The program won't know what textblock you are asking for. This is why an error is thrown.
You could use the collection that you bound to the listbox to get the customer name.
Related
Relatively new to WPF and binding. I have a Listbox filled with values. When a user selects a certain value I want to display details of that value over several labels and a textbox. The current LINQ query that fills my listbox list gets the whole table, so the data is inside that list. How do I go around passing the details of the selected value inside of a label?
My current code for the listbox is :
<ListBox x:Name="LBController" Grid.Column="1" HorizontalAlignment="Left" Grid.RowSpan="6" Grid.Row="1"
ItemsSource ="{Binding AllControllers}" SelectedValue="{Binding SelectedControllerID}"
SelectedValuePath="Id" DisplayMemberPath="Name" >
</ListBox>
If a value inside of the listbox is selected, I would like its name to be displayed on a seperate label
<Label x:Name="lbl_controllername"
Content="Controller Naam" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="1" Grid.Column="2" FontFamily="Corsiva" FontSize="11" Margin="40,0,0,0"/>
EDIT : Thanks for the answers everyone. This does however seem to prove difficult when doing the same thing with a control that has a function behind it.
<TextBox x:Name="txt_CodeDetail"
TextWrapping="Wrap"
AcceptsReturn="True"
FontFamily="Corsiva"
FontSize="11"
Text="{Binding ControllerCode, Mode=TwoWay}"/>
For example I have a textbox, the text inside of it is converted into a string for a function the app does. This string is called ControllerCode. This was done by copy-pasting the code before but I want the textbox to be filled with a selected property from the value inside the listbox.
I can't use
Text="{Binding SelectedItem.Property Elementname=LBController}"
as that would get rid of Controllercode and ruin the function. How can I bind the textbox text to the selecteditem as well as maintain the string that is formed with controllercode?
If you need to display several properties of an element in some area of the window, then it is easier in this area to set a panel in which the data context is bound to the selected element.
And inside this panel, set a set of elements to represent the properties of the selected element.
Example:
<StackPanel DataContext="{Binding SelectedItem, ElementName=LBController}">
<TextBlock Text="{Binding FirstProperty}"/>
<TextBlock Text="{Binding SecondProperty}"/>
<!--Other elements-->
</StackPanel>
I have a ListBox bound to a data context that works fine. But on some items I want to show a textblock with data from another context. But I cant get it to work. I had it working on a string in the cs class, but when I changed it to a class that implements INotifyPropertyChanged i dont get the text to show at all...
This is a simplified ItemTemplate of my xaml:
<DataTemplate x:Key="ArrivalFlightItemTemplate">
<TextBlock Text="{Binding ArrivalFlightItem.Operator}" />
<StackPanel Visibility="{Binding Converter={StaticResource FlightDisclaimerConverter}}">
<TextBlock Text="{Binding DisclaimerText}" Style="{StaticResource NormalText}" Foreground="{StaticResource DarkForegroundColor}">
<TextBlock.DataContext>
<local:FlightDisclaimerItem/>
</TextBlock.DataContext>
</TextBlock>
</StackPanel>
</DataTemplate>
In the cs constructor I have:
var flightsVM = SingletonClass.FlightsViewModel;
this.DataContext = listFlightsVM;
disclaimerItem = new FlightDisclaimerItem();
disclaimerItem.DisclaimerText = Strings.FlightInfoDisclaimerShort;
Can anyone please help me figure this out (I'm new to Windows Phone)?
Why are you binding some of your data in your code-behind? You should do it all in XAML.
Define disclaimerItem as a property in your ViewModel.
Set the name for your PhoneApplicationPage
Use the PhoneApplicationPage Name in your markup extension to bind the textblock in question to the ViewModel property
<TextBlock DataContext="{Binding DataContext.DisclaimerItemProperty, ElementName=phoneApplicationPageName}" />
I got it working! I couldnt get it to work using another DataContext in the DataTemplate. But by binding to another object in the page (disclaimer panel) it works as intended.
Text="{Binding ElementName=DisclaimerPanel, Path=DataContext.DisclaimerText}"
I currently have a list of objects in which my RadGridView's ItemsSource is set to. When the property "DoNotContact" of the object in the list has been set to True, I want to hide the information in the cell that contains a Phone number within my RadGridView. As you can see in my XAML, I'm setting the Visibility property within the TextBlock like so:
<telerik:GridViewDataColumn Header="Evening" DataMemberBinding="{Binding Path=EveningPhone}" Width="75" SortMemberPath="EveningPhone">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Visibility="{Binding Path=DoNotContact, Converter={StaticResource BoolToVisibilityConverter}}">
<Hyperlink Click="MakeEveningCallHandler">
<TextBlock Text="{Binding Path=EveningPhone}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
When attempting to debug it, the Converter is never hit and although I can see the property "DoNotContact" has been set, the phone number still shows. The converter itself works fine as I've used it in other occasions. Again I only want to hide the information WITHIN the cell for the "Evening" Property, not the actual column itself. Any Ideas what's going wrong here? Thanks a bunch!
The code you provided works for me!
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Getting value of a PasswordBox that's inside a ListView
I have a ListView that has a TextBox:
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="200" Name="tb" />
</DataTemplate>
</GridViewColumn.CellTemplate>
Can someone tell me how to get the value of tb for say ListViewItem no 3?
It easiest and most natural, from point of view of WPF, way is to use databinding, assigned to your control. For example:
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="200" Name="tb" Text="{Binding Path=MyModelViewProperty}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
Set MyModelViewProperty and text will appear on TextBox, change the TextBox Text porperty and you model view property: MyModelViewProperty will be changed. There is no need to get a value from control, that value will be pushed on you by Binding mechanism automatically. Here is a simple explanation of binding and how to implement it: Msdn Binding in WPF
Regards.
You can get the generated items from the ItemContainerGenerator, either by index or item, then you can use FindName on that item to get your TextBox.
I have a ComboBox in WPF which is databound, and has data template which controls how each of the items is displayed. I have made it so that each item is displayed with two bits of text (for the Name and Path properties) and one image (for the Icon property).
At the moment when I select an item from the ComboBox the textbox bit of the ComboBox just changes to say "TestWPF.Result" which is the name of the class which I have populated the ComboBox with.
I'm interested in one (or both) of two things:
How do I change it so that it displays the value of one of the fields there (eg. so it shows the value of the Name field rather than the name of the class)?
Is it possible get it to use the same DataTemplate there as in the list of items, so that once I have selected an item it displays in the closed ComboBox the same way as it looks in the list of items. Basically I've got a DataTemplate called ShowResults and a ComboBox which uses that template. I've also added in a separate ContentControl which I've got to show the details of the selected item in the ComboBox, but I want to get that to replace the textbox in the ComboBox.
Update:
Thanks for the first answer. I've tried using a separate ContentControl, as you've described, and it works fine. The question now is how to replace the textbox part of the ComboBox with this ContentControl. Any hints on that would be most welcome.
Also, is it possible to replace the textbox bit of the ComboBox control with a mixture of the ContentControl and a textbox, so that I can still type in the textbox to help select items from the ComboBox, but then when I close the dropdown the rest ContentControl bit will be populated with the rest of the text and the icon. Hope that makes sense - ask questions if it doesn't!
Code:
I've been asked to post my code - so here it is. I've tried to remove things that I know are definitely not relevant, but I'm not sure exactly what is relevant so when in doubt I've left things in.
<Window x:Class="TestWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:TestWPF"
Title="Window1" Height="300" Width="843" Loaded="Window_Loaded">
<Window.Resources>
<DataTemplate x:Key="ShowResult" DataType="TestWPF.Result">
<StackPanel Margin="5" Orientation="Horizontal">
<Image Width="32" Height="32" Source="{Binding Path=Image}"/>
<StackPanel Margin="5">
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
<TextBlock Text="{Binding Path=Path}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid Width="786">
<Button Height="23" HorizontalAlignment="Right" Margin="0,24,166,0" Name="btnTest" VerticalAlignment="Top" Width="75" Click="btnTest_Click">Add</Button>
<ComboBox StaysOpenOnEdit="True" DropDownClosed="comboBox1_DropDownClosed" PreviewTextInput="comboBox1_PreviewTextInput" SelectionChanged="comboBox1_SelectionChanged" ItemTemplate="{StaticResource ShowResult}" Margin="259,109,22,89" Name="comboBox1" IsEditable="True" />
<ContentControl Height="50" Margin="268,0,22,21" Name="contentControl1" VerticalAlignment="Bottom" Content="{Binding ElementName=comboBox1,Path=SelectedValue}" ContentTemplate="{StaticResource ShowResult}"/>
</Grid>
You got the binding part right - binding to the data and using a DataTemplate to display the source the way you want to.
As to your second question, a way to do it would be to use a ComboBox with IsEditable="True" as you have, and withing the TextChanged event handler check if the comboBox.Items contains the new value, if not check use Linq to seach for a match:
if (comboBox.Items.Contains(e.NewValue))
return;
var matches =
with comboBox.Items
select item
where item.BeginsWith(e.NewValue);
if (matches.Count > 0)
comboBox.SelectedItem = matches.First();
Just place the Property Binding expression to the textBox,You dont need to apply template.
Another way to get exact Data template, Place a ContentControl in the place of textBox and assign the same DataTemplate (say x:Name="robinTemplate")
<ContentControl Content="{Binding ElementName=cmbBox,Path=SelectedValue}" ContentTemplate="{StaticResource robinTemplate}"/>
For making the Selected content display in the same way :
Create a copy of the combobox control template and you will find a ContentPresenter there. Replace that with the ContentControl.. This is not the right solution though.