Answered by:
Getting null when IValueConverter???

Question
-
public object Convert(object value, Type targetType, object parameter, string language) { TextBlock txt = new TextBlock(); string str = (string)value; string[] strList = str.Split('=>'); Run run2 = new Run(); SolidColorBrush mySolidColorBrush = new SolidColorBrush(Color.FromArgb(255,0,0,255)); run2.Foreground = mySolidColorBrush; txt.Inlines.Add(run2); return txt; } public object ConvertBack(object value, Type targetType, object parameter, string language) { return false; }
if(i< orth.Count()) { results.senselist += type_translation[i].InnerText.Trim() + "=>" + orth[i].InnerText.Trim() + " "; } else { results.senselist += type_translation[i].InnerText.Trim() + "|"; }
<TextBlock Text="{Binding senselist,Converter={StaticResource textconvert}}"></TextBlock>
On unversial app not suport <label> so I use Textblock. but
string str = (string)value; string[] strList = str.Split('=>');
it is null.
Monday, April 20, 2015 6:56 AM
Answers
-
If value is NULL in your Convert method, the binding to the senselist property has failed. Make sure that the DataContext of the TextBlock contains a public property named "senselist" and that this property actually returns a string value.
I also notice that you are trying to return a TextBlock from the converter but the Text property of a TextBlock cannot be set to another TextBlock.
You should awlays return a string from the converter. If you want to display the TextBlock in different colours you could bind the Text property of the Run element(s) and for example return a substring of the source string from the converter:
<TextBlock> <Run Text="default colour..." /> <Run Text="{Binding senselist,Converter={StaticResource textconvert}}" Foreground="Green" /> </TextBlock>
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.- Marked as answer by Le Thien Hoang Monday, April 20, 2015 2:05 PM
Monday, April 20, 2015 1:53 PM -
As I told you, you should return a string an not a TextBlock from the converter. I don't know what you are trying to with the converter though. You are for example not setting the Text property of the Run that you create in there...
Just return a string:
public object Convert(object value, Type targetType, object parameter, string language) { string str = (string)value; string[] strList = str.Split('=>'); if(strList.Count > 1) return strList[1]; return str; } public object ConvertBack(object value, Type targetType, object parameter, string language) { return false; }
Please remember mark all helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.- Marked as answer by Le Thien Hoang Tuesday, April 21, 2015 8:54 AM
- Unmarked as answer by Le Thien Hoang Tuesday, April 21, 2015 8:58 AM
- Marked as answer by Le Thien Hoang Tuesday, April 21, 2015 8:58 AM
Tuesday, April 21, 2015 8:07 AM
All replies
-
If value is NULL in your Convert method, the binding to the senselist property has failed. Make sure that the DataContext of the TextBlock contains a public property named "senselist" and that this property actually returns a string value.
I also notice that you are trying to return a TextBlock from the converter but the Text property of a TextBlock cannot be set to another TextBlock.
You should awlays return a string from the converter. If you want to display the TextBlock in different colours you could bind the Text property of the Run element(s) and for example return a substring of the source string from the converter:
<TextBlock> <Run Text="default colour..." /> <Run Text="{Binding senselist,Converter={StaticResource textconvert}}" Foreground="Green" /> </TextBlock>
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.- Marked as answer by Le Thien Hoang Monday, April 20, 2015 2:05 PM
Monday, April 20, 2015 1:53 PM -
As results is "
Windows.UI.Xaml.Controls.TextBlock
". I had debugger then returntxt
isnull
while "txt.Inlines.Add(run2);
" is having data. Everyone help me to solve it.
- Edited by Le Thien Hoang Tuesday, April 21, 2015 3:32 AM
Monday, April 20, 2015 2:08 PM -
public object Convert(object value, Type targetType, object parameter, string language) { TextBlock txt = new TextBlock(); string str = (string)value; string[] strList = str.Split('=>'); Run run2 = new Run(); SolidColorBrush mySolidColorBrush = new SolidColorBrush(Color.FromArgb(255,0,0,255)); run2.Foreground = mySolidColorBrush; txt.Inlines.Add(run2); return txt; } public object ConvertBack(object value, Type targetType, object parameter, string language) { return false; } <TextBlock> <Run Text="{Binding senselist,Converter={StaticResource textconvert}}" Foreground="Green" /> </TextBlock>
As results is "Windows.UI.Xaml.Controls.TextBlock
". I had debugger then returntxt
isnull
while "txt.Inlines.Add(run2);
" is having data. Everyone help me to solve it.- Merged by Rob Caplan [MSFT]Microsoft employee, Moderator Tuesday, April 21, 2015 5:30 AM duplicate
Tuesday, April 21, 2015 5:28 AM -
As I told you, you should return a string an not a TextBlock from the converter. I don't know what you are trying to with the converter though. You are for example not setting the Text property of the Run that you create in there...
Just return a string:
public object Convert(object value, Type targetType, object parameter, string language) { string str = (string)value; string[] strList = str.Split('=>'); if(strList.Count > 1) return strList[1]; return str; } public object ConvertBack(object value, Type targetType, object parameter, string language) { return false; }
Please remember mark all helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.- Marked as answer by Le Thien Hoang Tuesday, April 21, 2015 8:54 AM
- Unmarked as answer by Le Thien Hoang Tuesday, April 21, 2015 8:58 AM
- Marked as answer by Le Thien Hoang Tuesday, April 21, 2015 8:58 AM
Tuesday, April 21, 2015 8:07 AM -
<TextBlock> <Run Text="default colour..." />// Here is binding senselist but foreground is defaults??? but It also show fs up. <Run Text="{Binding senselist,Converter={StaticResource textconvert}}" Foreground="Green" /> </TextBlock>
I still not understanding.
I have a string is "abc=fs"
I trying to your code then it just show "fs" up. I want both.
<Run Text="default colour..." /> is "abc"
<Run Text="{Binding senselist,Converter={StaticResource textconvert}}" Foreground="Green" /> is "fs"
- Edited by Le Thien Hoang Tuesday, April 21, 2015 9:11 AM
Tuesday, April 21, 2015 8:57 AM