{"record":{"id":"51a9134fbb363235","repo":"dotnet/wpf","slug":"sr-format-sr-propertyconditionincorrecttype-property","errorCode":null,"errorMessage":"SR.Format(SR.PropertyConditionIncorrectType, property.ProgrammaticName, expectedType.Name)","messagePattern":"SR\\.Format\\(SR\\.PropertyConditionIncorrectType, property\\.ProgrammaticName, expectedType\\.Name\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/ItemContainerPattern.cs","lineNumber":140,"sourceCode":"       #region Private Methods\n\n        private object PropertyValueValidateAndMap(AutomationProperty property, object value)\n        {\n            AutomationPropertyInfo info;\n            if (!Schema.GetPropertyInfo(property, out info))\n            {\n                throw new ArgumentException(SR.UnsupportedProperty);\n            }\n\n            // Check type is appropriate: NotSupported is allowed against any property,\n            // null is allowed for any reference type (ie not for value types), otherwise\n            // type must be assignable from expected type.\n            Type expectedType = info.Type;\n            if (value != AutomationElement.NotSupported &&\n                ((value == null && expectedType.IsValueType)\n                || (value != null && !expectedType.IsAssignableFrom(value.GetType()))))\n            {\n                throw new ArgumentException(SR.Format(SR.PropertyConditionIncorrectType, property.ProgrammaticName, expectedType.Name));\n            }\n\n            // Some types are handled differently in managed vs unmanaged - handle those here...\n            if (value is AutomationElement)\n            {\n                // If this is a comparison against a Raw/LogicalElement,\n                // save the runtime ID instead of the element so that we\n                // can take it cross-proc if needed.\n                value = ((AutomationElement)value).GetRuntimeId();\n            }\n            else if (value is ControlType)\n            {\n                // If this is a control type, use the ID, not the CLR object\n                value = ((ControlType)value).Id;\n            }\n            else if (value is Rect rc)\n            {\n                value = new double[] { rc.Left, rc.Top, rc.Width, rc.Height };","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/ItemContainerPattern.cs#L122-L158","documentation":"FindItemByProperty (via PropertyValueValidateAndMap) checks that the condition value's runtime type is compatible with the property's expected type: null is only allowed for reference types, and otherwise value.GetType() must be assignable to the expected type. A mismatch throws ArgumentException with a message naming the property and expected type.","triggerScenarios":"Passing a string where the property expects double (e.g. RangeValuePatternIdentifiers.ValueProperty); passing a value-type null (e.g. null for a bool property); passing the wrong enum/control-type object type to a property comparison.","commonSituations":"Stringly-typed configuration supplying property values from user input; binding UI values (always strings) into FindItemByProperty; refactoring that changed a property's expected type; culture/number parsing producing wrong types.","solutions":["Convert the value to the property's expected type before calling (e.g. double.Parse for numeric properties)","Never pass null for value-type properties; use AutomationElement.NotSupported to match all items","Use pattern matching / Convert.ChangeType against the property info's Type"],"exampleFix":"// before\npattern.FindItemByProperty(null, RangeValuePatternIdentifiers.ValueProperty, \"50\"); // string vs double\n// after\npattern.FindItemByProperty(null, RangeValuePatternIdentifiers.ValueProperty, 50.0);","handlingStrategy":"validation","validationCode":"if (value != AutomationElement.NotSupported && value != null && !expectedType.IsInstanceOfType(value))\n    throw new ArgumentException($\"{property.ProgrammaticName} expects {expectedType.Name}\");","typeGuard":"bool IsValidConditionValue(AutomationProperty p, object v) => v == AutomationElement.NotSupported || (v != null && p.GetType() != null && v is not null && v.GetType() != typeof(object) && !(v is string && p == AutomationElement.ControlTypeProperty));","tryCatchPattern":"try { pattern.FindItemByProperty(null, prop, value); } catch (ArgumentException) { value = ConvertValue(prop, value); }","preventionTips":["Coerce values (Convert.ChangeType) to the property's expected type","Never pass null for value-type properties","Parse strings from UI/config before passing as condition values"],"tags":["uiautomation","itemcontainer-pattern","type-mismatch","argument-exception"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}