Getting the name of a resource from code behind - wpf

I have a TextBlock with the following xaml:
<TextBlock x:Name="typeTextBlock" Text="{DynamicResource TypeString}" ></TextBlock>
I want to get the string "TypeString" in code behind and assign it to a variable of type string. How can I achieve that?
string typeResource = ???
Now typeResource variable should be equal to the string "TypeString"
NOTE:
I don't want to get the value of the resource, but the name of the resource:
"TypeString": "Type",
I don't want the string "Type", but the string: "TypeString".

Try this:
string typeResource = Application.Current.Resources["TypeString"] as string;
Update: hopefully now I get the question correctly:
string typeResource = typeTextBlock.GetBindingExpression(TextBlock.TextProperty).ResolvedSourcePropertyName;
Update 2: Just came across another method:
string typeResource = BindingOperations.GetBinding(typeTextBlock, TextBlock.TextProperty).Path.Path;

Related

How to filter String Array in VB.Net?

What is VB.Net code to filter a String Array ?
I use following code
Imports System.Reflection
Dim ass As Assembly = Assembly.GetExecutingAssembly()
Dim resourceName() As String = ass.GetManifestResourceNames()
that return a String array
How can I filter resourceName() variable ?
I tried following lines of code
Dim sNameList() As String
= resourceName.FindAll(Function(x As String) x.EndsWith("JavaScript.js"))
but compiler return following error
BC36625: Lambda expression cannot be converted to 'T()' because 'T()' is not a delegate type
How can I correct this error ?
Is there another solution to solve my problem ?
Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js"))
In that case, sNameList is an IEnumerable(Of String), which is all you need if you intend to use a For Each loop over it. If you genuinely need an array:
Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js")).ToArray()
The reason that your existing code didn't work is that Array.FindAll is Shared and so you call it on the Array class, not an array instance:
Dim sNameList = Array.FindAll(resourceName, Function(s) s.EndsWith("JavaScript.js"))

Is there a simpler way to decode this json in Go?

I am trying to parse some JSON from Jira to variables. This is using the go-jira package (https://godoc.org/github.com/andygrunwald/go-jira)
Currently I have some code to get the developer:
dev := jiraIssue.Fields.Unknowns["customfield_11343"].(map[string]interface{})["name"]
and team := jiraIssue.Fields.Unknowns["customfield_12046"].([]interface{})[0].(map[string]interface{})["value"]
to get the team they are a part of from.
Getting the team they are on is a bit gross, is there a cleaner way to get the team besides having to type assert, set the index, then type assert again?
Here is the complete json (modified but structure is same, its way too long):
{
"expand":"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id":"136944",
"self":"https://jira.redacted.com/rest/api/2/issue/136944",
"key":"RM-2506",
"fields":{
"customfield_11343":{
"self":"https://redacted.com/rest/api/2/user?username=flast",
"name":"flast",
"key":"flast",
"emailAddress":"flast#redacted.com",
"displayName":"first last",
"active":true,
"timeZone":"Europe/London"
},
"customfield_12046":[
{
"self":"https://jira.redacted.com/rest/api/2/customFieldOption/12045",
"value":"diy",
"id":"12045"
}
],
}
Thanks
The way I go about problems like this is:
Copy some JSON with things I am interested in and paste it into https://mholt.github.io/json-to-go/
Remove fields that arenĀ“t of interest.
Just read the data and unmarshal.
You might end up with something like this given the two custom fields of interest, but you can cut the structure down further if you just need the name.
type AutoGenerated struct {
Fields struct {
Customfield11343 struct {
Self string `json:"self"`
Name string `json:"name"`
Key string `json:"key"`
EmailAddress string `json:"emailAddress"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
} `json:"customfield_11343"`
Customfield12046 []struct {
Self string `json:"self"`
Value string `json:"value"`
ID string `json:"id"`
} `json:"customfield_12046"`
} `json:"fields"`
}
The effect you get is that all extra information in the feed is discarded, but you get the data you want very cleanly.
This is a tough one since the second one is in an array form. It makes it hard to use a map.
For the first one, it's simple enough to use:
type JiraCustomField struct {
Self string `json:"self"`
Name string `json:"name"`
Key string `json:"key"`
EmailAddress string `json:"emailAddress"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
}
type JiraPayload struct {
Expand string `json:"expand"`
ID string `json:"id"`
Key string `json:"key"`
Fields map[string]JiraCustomField `json:"fields"`
}
https://play.golang.org/p/y8-g6r0kInV
Specifically this part Fields map[string]JiraCustomField for the second case it looks like you need it in an array form like Fields map[string][]JiraCustomField.
In a case like this, I think you'll need to make your own Unmarshaler. This is a good tutorial: https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/
What you could do with your custom Unmarshal/marshaler, is use the Reflection package and check if it's an array or a struct. If it's a struct then put it into an array, and store it in Fields map[string][]JiraCustomField.

How can I convert collection laravel to array?

My code like this :
$test = $this->vendorRepository->getVendor($request->get('q'));
If I dd($test), the result is collection like this :
I want to convert it to array
I try like this :
dd($test->toArray());
The result like this :
value of id changed to 0
Why did it happen? How can I solve this problem?
This might be because your ID field is a string but laravel is expecting it to be an auto-incrementing integer.
Try adding this to the top of your model:
public $incrementing = false;

How to parse SQL record to Enum

Is there a way to parse enum value from table record. For example, I have class which contains user data, and on of them is Enum type. The data is passed from DataRow, but I have trouble parsing enum value.
I tried something like this,
uType= (EType) Enum.TryParse(typeof(row["userType"]));
but it wouldn't compile. Any tip?
Thanks.
try with this code
uType = (EType) Enum.Parse(typeof(EType), row["userType"].ToString(), true);
Enum.TryParse returns a boolean that indicates if the value could be parsed successfully.
Assuming that userType is a string in the DataTable:
EType eType;
bool canParse = Enum.TryParse(row.Field<String>("userType"), out eType);

Find a word in window

How can i find a word in a string that im searching with :
myProcess.MainWindowTitle
Lets say that i want :
if myProcess.MainWindowTitle = something.contains("calc.exe") then
or
if myProcess.MainWindowTitle = contains.myproccessNAme then
Process.MainWindowTitle is a string, so you can simply use String.Contains method:
if (myProcess.MainWindowTitle.Contains("calc.exe")) then

Resources